Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hi, I'm very new to unix. About

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hi,

I'm very new to unix. About 2 months.

I'm trying to write a script that will check whether users mentioned as arguments are logged onto the system.

I've been reading about rusers but don't no where to go with this.

Any tips?
 
On most unix systems, the command "w" will return a list of users logged in, in this format:
Code:
USER             TTY      FROM              LOGIN@  IDLE WHAT
rick             v0       {terminal}        28Dec01 8days -bash (bash)
rain_e           p0       {hostname}        Fri04PM     - -bash (bash)

(try "man w" for more options)

If you specify usernames as arguments, it will filter the list only to show those names among the logged in users. -------------------------------------------

"Calculus is just the meaningless manipulation of higher symbols"
                          -unknown F student
 
well i'm trying to write the script so at the command line i can type:

scriptname username

returns:

username is logged in


this is what i got so far:

if [ userid = "username" ]
echo logged in
else
logged out

but i get nothing.

 
Hi rain_e,

You can use this little script :

TestUser

User=${1:?"Usage: $0 username"}
if [ -z "$(who | grep $User)" ]
then echo "$User : logged out"
else echo "$User : logged in"
fi
Jean Pierre.
 
how would i accept more than 1 argument.

if i wanted to test for 2 users at the same time?
 
The first script TestUser will check whether users are logged onto the system.

The second script TestAllUsers will check whether all users are logged at the same time.

TestUser

if [ $# -eq 0 ] ; then
echo "Usage: $0 username [username...]"
exit 1
fi
for User in $*
do
if [ -z "$(who | grep $User)" ]
then echo "$User : logged out"
else echo "$User : logged in"
fi
done

TestUser

if [ $# -eq 0 ] ; then
echo "Usage: $0 username [username...]"
exit 1
fi
for User in $*
do
if [ -z "$(who | grep $User)" ] ; then
echo "At least one user not logged in : $User"
exit
fi
done
echo "All users are logged in : $*" Jean Pierre.
 
another helpful command is last. The last command will give you a list of all the users that have logged into the system, when they logged in, and for how long, or if they are still logged in it will tell you.

Since the listing is very long, I usually use
last | head

or
last | grep still

to see who is currently logged in.

Hope this helps. Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top