I am working on a simple login ID check shell script that should prompt for a user ID then check to see if this user is logged on. Trying to get the hang of this stuff so I am thinking of my own little projects.
#! /bin/sh
echo "please enter a user name"
read user
if user=$user
then
do
User=`who |grep user1,user2,user3|cut -d " " -f1`
echo User ID = $User
fi
done
The script worked as you have it. Thanks for the help... but what I was looking for is a way to tell if the person is currently logged in and give an echo to when they logged in. Sorry I should have been more concise!
One idea is to count the number of times a user appears in the "who -u" list by using the "wc -l" command (which just counts the number of lines it sees).
The backticks ` ` are important and cause the commands to be executed. Note the correct syntax for an "if" statement. Not clear where the "do" and "done" figure in Post #1.
#!/bin/sh
echo "please enter a user name"
read user
user_login_count=`who -u | grep "^${user} " | wc -l`
if [ ${user_login_count} -gt 0 ]
then
echo "User ${user} is logged in ${user_login_count} times"
# Show some evidence
who -u | grep "^${user} " | awk '{print $1}'
else
echo "User ${user} is not logged in"
fi
If you have a modern POSIX shell, the line containing the backticks can be replaced with the more eloquent: