Shell Script to Check if the User is Logged in or Not

shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script that sets up the environment, runs the program, and does any necessary cleanup, logging, etc. is called a wrapper.

In this post, we will write a shell script to check if the user provided by the input is logged in or not.

INPUT:
The first line contains one string: username

OUTPUT:
Print “logged in” the user is logged in else print “not logged in”

The following is the shell script to check if the user is logged in or not:

echo enter username
read name
who > test
if grep $name test
then
echo logged in
else
echo not logged in
fi

OUTPUT:

$ enter username
$ test
$ test    tty1         2019-02-21 10:56 (:0)
$ test    pts/0        2019-02-21 10:57 (:0)
$ test    pts/1        2019-02-26 19:19 (:0)
$ logged in

Let us know in the comments if you are having any questions regarding this shell script.

And if you found this post helpful, then please help us by sharing this post with your friends. Thank You

Leave a Reply