The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-16-2008
akilayko akilayko is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
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 (permalink)  
Old 01-16-2008
bc5989 bc5989 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 7
I assume you have defined $uname before this line else grep has no idea what it is.
  #3 (permalink)  
Old 01-16-2008
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
  
 

Join Date: Dec 2007
Posts: 430
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 (permalink)  
Old 01-16-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557
Quote:
Originally Posted by akilayko View Post
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 (permalink)  
Old 01-17-2008
akilayko akilayko is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
thank you frank_rizzo & ghostdog74 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 (permalink)  
Old 01-17-2008
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
  
 

Join Date: Dec 2007
Posts: 430
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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0