This is for my linux class.
Write a script called activity4.2-3 that modifies your activity4.2-2 script to also display a message when the user provided is not logged on. An example is shown below. Purpose: Determine if someone is logged on bash-3.2$ ./activity4.2-3 nonuser nonuser is either not logged on or not a valid user Include a first line that calls the bash shell as the interpreter Add a comment to state the purpose of the script and other comments to make the script easier to read
#!/bin/bash
# activity4.2-3.sh
# Usage: activity4.2-3.sh username
# Shell script which checks after every one minute whether a user
has logged in
# or not
# Can be run in background using & then foreground it to view
result
if [ $# -ne 1 ]
then
echo "Wrong arguments !!!!!!"
echo "usage : `basename $0` username"
exit 1
fi
isUserExist()
{
grep -w "$1" /etc/passwd > /dev/null
if [ $? -eq 1 ]
then
echo "User $1 is not a valid user"
exit 1
fi
}
isUserExist $1
time=0
while true
do
who|grep $1 > /dev/null
if [ $? -eq 0 ]
then
echo "User $1 is logged in "
exit 0
else
let time++
# This indicates the waiting time. Can be changed as
required.
sleep 60
fi
done
OUTPUT:
[root@localhost shell]# sh activity4.2-3.sh
Wrong arguments !!!!!!
usage : activity4.2-3.sh username
[root@localhost shell]# sh activity4.2-3.sh root
User root is logged in
[root@localhost shell]# sh activity4.2-3.sh tttt
User tttt is not a valid user
Get Answers For Free
Most questions answered within 1 hours.