Hi,
I'm taking a Linux course.
One task is creating a script to check whether a user is logged in or not.
The script run with useame for argument. If there is none, it is to be prompted for.
The script is to exit 0 if user is logged in, and exit 1 if not.
Code:
#!/bin/bash
function checkUser
{
for u in $(who | awk '{print $1}' | sort | uniq)
do
if [ "$u"="$user" ]; then
echo "User $u is logged in."
echo "Exit status: $?"
exit $?
else
"User $u is not logged in."
echo "Exit status: $?"
exit $?
fi
done
}
if [ -z $1 ]; then
echo "User to check:"
read user
else
"$user"="$1"
fi
checkUser $1
Code:
$ ./checkuser henrix .checkuser: line23: =henrix: command not found User adam is logged in. Exit status 0.
Code:
$ ./checkuser User to check: henrix User adam is logged in. Exit status 0.
Code:
$ who | awk '{print $1}' | sort | uniq
adam
henrix
This is a follow-up to http://ubuntuforums.org/showthread.php?t=2075873 (closed).
برچسب:
نویسنده: استخدام کار