Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a bash script in linux that determines if a particular user (can be any us

ID: 655171 • Letter: W

Question

Write a bash script in linux that determines if a particular user (can be any user you want) is logged on. The script will be executed with an argument, which is a username. An example is shown below.

Purpose: Determine if someone is logged on

The script is executed with an argument as such:

bash-3.2$ ./myscript userbob

The script needs to determine if the user is logged in, and output something like this:

userbob is logged on

Or if the user is not logged on, or doesn't exist, it should output something like:

userbob is not logged it

Explanation / Answer

#!/bin/bash

#define my function

function checkuser

{

# for each variable in this command it will do

        for u in `who | grep $u`

        do

        if [ $u = $1 ] ; then

                exit 0

        fi

        done

}

# if no argument was given ask for username.

if [ -z $1 ] ; then

        echo "Enter username: "

        read u

# sends input to function

        checkuser $u

        exit 1

fi

checkuser $1

$ echo '$u is logged in'

exit 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote