Bash Help: users who are not logged into the system to display


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Help: users who are not logged into the system to display
# 1  
Old 07-18-2009
Bash Help: users who are not logged into the system to display

A Newbie here,

I am working on a script and am having problems with the else part of the script. I can't get the users who are not logged into the system to display on the screen with their username and the text "The user is not logged in". I am sure it is something simple and stupid, but I can't find out what my issue is.

Please help.

Thanks,

Rob


Code:
users=`cat /etc/passwd | cut -d: -f1`
for i in $users
do
isloggedin=`who | grep $i | head -1`
if [ -n isloggedin ]
then
echo $isloggedin
else
echo $users
fi
done

# 2  
Old 07-18-2009
Tools take a look at your variables

In your if statement, you were not referring to isloggedin with the $

Code:
users=`cat /etc/passwd | cut -d: -f1`
for i in $users
do
  isloggedin=`who | grep $i | head -1`
  if [ -n $isloggedin ]
    then
      echo $isloggedin
    else
      echo $users
  fi
done

# 3  
Old 07-18-2009
Joey G,

I put the $ in where you suggested, and it still does not work right. Any other ideas?

Thanks for your help.

Rob
# 4  
Old 07-18-2009
With awk:

Code:
awk 'BEGIN{while("who" | getline)w[$1]}
$1 in w {print $1 " is logged in"; next}
{print $1 " is NOT logged in"}
' FS=":" /etc/passwd

# 5  
Old 07-18-2009
Try this command .....

grep -v `who | tr -s " " | cut -d" " -f1` /etc/passwd | awk -F":" '{print $1}'
# 6  
Old 07-18-2009
Thanks everyone for your help!

I appreciate it

Rob
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

[Tip] How to display the number of logged-in users

In a professional environment with traditional application you often want (or are asked) to report the users. Traditionally there is the who command who | awk '{print $1}'telnetd or sshd register the users in the utmp file, to be shown with who, w, users, finger, pinky, ... In addition they... (1 Reply)
Discussion started by: MadeInGermany
1 Replies

2. UNIX for Dummies Questions & Answers

Users logged into the system

So I'm trying to write a single line command So I have to use last first in this command and I've figured out the format my professor wants it in, something like thislast | cut -d' ' -f1,15 | sort > check | uniq -c.... and I never can get it right, when I just last command I get something... (2 Replies)
Discussion started by: DoubleAlpha
2 Replies

3. Shell Programming and Scripting

(RHEL, Bash) List users and check if they have logged on during the last 2 months

Hi everyone, At work we were told to check the list of users of an application server and delete all those that have left the company or don't need access to the application anymore. Here's what I came up with. Would you be as kind as to tell me your opinion and whether there is a faster / easier... (4 Replies)
Discussion started by: gacanepa
4 Replies

4. UNIX for Dummies Questions & Answers

How many users are logged in?

How do I find this out? I have a feeling its a simple command such as who, but I just don't know what it is. I've had a search on here but either I can't put it into the right search criteria or there isn't a topic on it. Thanks. EDIT: Delete this thread, as I posted it I noticed the... (0 Replies)
Discussion started by: chris_rabz
0 Replies

5. Programming

Get the list of logged in users

How can I get the list of logged in users in the system programmatically? I can get the list with 'who' or 'users' commands but I need to get the list programmatically... May someone help, please? Thanks in advance. (2 Replies)
Discussion started by: xyzt
2 Replies

6. Shell Programming and Scripting

Users Not Logged in

I have searched the forums but have not mangaed to quite find what im looking for. I have used to /etc/passwd command to present me a list of all users the who command to present all users currently logged on, but what i want to know is what command can i use to display users that are registered... (12 Replies)
Discussion started by: warlock129
12 Replies

7. Post Here to Contact Site Administrators and Moderators

logged out users

how to find out users who logged out within 5 minutes (1 Reply)
Discussion started by: roshni
1 Replies

8. Shell Programming and Scripting

how many users logged

in unix what is the syntax to find out how many users are currently logged in (4 Replies)
Discussion started by: trichyselva
4 Replies

9. IP Networking

Users logged in through which NIC

We have two NIC cards in our IBM RS/6000 F50 running AIX 4.3.3 We are trying to make sure we have moved all users to log in through the new NIC. 10.22.x.y (old) 10.22.x.z (new) How can I tell which users are still using the old address for logging in so I can update their work station to... (5 Replies)
Discussion started by: cburtgo
5 Replies
Login or Register to Ask a Question