LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-29-2011, 11:13 AM   #1
SecureScript
LQ Newbie
 
Registered: Nov 2011
Posts: 6

Rep: Reputation: Disabled
If Statement for User Status


I am writing a bash script to let the user check if a specific username is currently logged in. However my script returns that "the username is logged in" even if they are not. I figure it has to be a problem with my If statement. Here is my code:

#========================================================
#!/bin/bash

clear
tput cup 2 1; echo "Enter username: "
tput cup 2 25;
read choice || continue

userchoice=`who | grep $choice | cut -d' ' -f1`
tput cup 5 14; echo "User $userchoice is logged on."

if [ $userchoice = 0 ]
then
tput cup 5 14; echo "User $userchoice is not logged on."
fi

exit 0
#============================================================

Any help is appreciated. Thanks!
 
Old 11-29-2011, 11:27 AM   #2
kbscores
Member
 
Registered: Oct 2011
Location: USA
Distribution: Red Hat
Posts: 259
Blog Entries: 9

Rep: Reputation: 32
It will always print that User is logged in because it is not in an if statement at all

you need to do

Code:
if [ $userchoice = 0 ]
then tput cup 5 14; echo "User $userchoice is not logged on."
else tput cup 5 14; echo "User $userchoice is logged on."
fi
 
Old 11-29-2011, 11:47 AM   #3
SecureScript
LQ Newbie
 
Registered: Nov 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Ok thanks kbscores, that is the correct If statement, however when I run it I get an error "Too many arguments" on the line starting with the If statement:

Code:
if [ $userchoice = 0]
 
Old 11-29-2011, 12:02 PM   #4
kbscores
Member
 
Registered: Oct 2011
Location: USA
Distribution: Red Hat
Posts: 259
Blog Entries: 9

Rep: Reputation: 32
Most likely it is returning several instances of a single user - for instance if you had three different shell windows open the who cut statement and your username is jbob it would return:

jbob
jbob
jbob

Since you aren't saving it to an array it is going to give an error. Try declaring variable first using:
Code:
declare -a userchoice
and then when you reference it use first instance of name so in if statement it would look like:
Code:
if [ $userchoice[0] = 0 ]
I'm thinking you may want to use null instead of zero as well.
 
Old 11-29-2011, 12:42 PM   #5
SecureScript
LQ Newbie
 
Registered: Nov 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
I did declare the array with null values as you said, but it still comes back saying user is logged in every time.

I'm thinking though, I already have userchoice declared (towards the top of the script):

Code:
userchoice=`who | grep $choice | cut -d' ' -f1`
Could that be conflicting with the declare -a userchoice that you suggested?
 
Old 11-29-2011, 12:49 PM   #6
kbscores
Member
 
Registered: Oct 2011
Location: USA
Distribution: Red Hat
Posts: 259
Blog Entries: 9

Rep: Reputation: 32
actually now that I think about it since you only need one use a sort -u

it is sort of long winded but it'll work.

I just wrote out script - I wouldnt use variable at all if you use sort.
 
Old 11-29-2011, 12:53 PM   #7
kbscores
Member
 
Registered: Oct 2011
Location: USA
Distribution: Red Hat
Posts: 259
Blog Entries: 9

Rep: Reputation: 32
this is what i did and it worked:

Code:
#!/bin/bash

echo "Enter name:"
read choice || continue 
if ["$(who |grep $choice | cut -d ' ' -f1 |sort -u)" == "$choice" ]
then echo "User $choice is online"
else echo "User $choice is not online"
fi
exit 0
 
Old 11-29-2011, 05:54 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
As we are using bash, why not just keep it simple:
Code:
if who | grep -q "$choice"
then
    echo "User $choice is online"
else
    echo "User $choice is offline"
fi
 
Old 11-30-2011, 07:44 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by SecureScript View Post
Ok thanks kbscores, that is the correct If statement, however when I run it I get an error "Too many arguments" on the line starting with the If statement:

Code:
if [ $userchoice = 0]
There needs to be a space before the final bracket. Remember, [ is a command, and all the pieces following the initial bracket are arguments to that command which need to be separated by whitespace in order for the shell to parse it correctly. In this case "0]" is treated as a single argument, meaning it can't find the closing bracket and thinks that everything following it is also part of the test.

In addition, always quote your variable substitutions, particularly inside [..]. A variable containing multiple words would otherwise be seen as multiple arguments.

More on argument processing here:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


Finally, you can avoid most of these problems in bash by using the new [[ test keyword instead.

But in fact, numeric comparisons like this are better done with the ((..)) arithmetic evaluation brackets.

Code:
if (( userchoice == 0 )) ; then
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Check User Account Status in Solaris devUnix Solaris / OpenSolaris 1 03-28-2011 03:15 PM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
Workarea Issue - - Notify user with status sahil.jammu Linux - Newbie 1 04-03-2009 08:24 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
User logging status shankarjha Linux - Newbie 2 04-17-2006 03:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration