if-then


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers if-then
# 1  
Old 01-16-2008
if-then

hello.

can someone please help me find out what is wrong with this if-then statement? this is a bourne shell script.

if [ w -h | grep $uname | wc -l = 0 ]; then
exit 0
fi

thank you for your help
# 2  
Old 01-16-2008
I assume you have defined $uname before this line else grep has no idea what it is.
# 3  
Old 01-16-2008
That is not a good way to do what you need.

how about this? You may want add the -w grep option depending on your requirements. This can prevent false positives. See the man page for details.

Code:
if w -h | grep -q $uname 
then
   exit 0
fi

or
Code:
w -h |grep -q $uname || exit 0

# 4  
Old 01-16-2008
Quote:
Originally Posted by akilayko
hello.

can someone please help me find out what is wrong with this if-then statement? this is a bourne shell script.

if [ w -h | grep $uname | wc -l = 0 ]; then
exit 0
fi

thank you for your help
Code:
if [ `w -h | grep root | wc -l` -eq 0 ];then
 echo "do something"
fi

# 5  
Old 01-17-2008
thank you frank_rizzo & ghostdog74 Smilie syntax-wise the code works. but...the wc is counting 1 when its supposed to count 0. this code is a part of a script that is supposed to weed out invalid users. if the $uname is invalid then there will be no processes running under that user. i thought of adding grep -v grep and the count is now 0. but that doesn't make sense.

thanks again.
# 6  
Old 01-17-2008
there is no reason to count the lines. you really only care if there is a match right?

try this

Code:
if w -h | grep -q -w  ^$uname 
then
   exit 0
fi

 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question