LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-17-2002, 08:50 AM   #1
dguy
Member
 
Registered: Dec 2001
Location: Beckwith Township, Ontario, Canada
Distribution: Slackware 14.0, Slackware-64 14.1
Posts: 119

Rep: Reputation: 16
Question bash scripting - conditionals


Hi...

Could someone please tell me how to go about testing the return value of "program | grep -c 'string'" in a shell script?

I'm practically a virgin when it comes to writing scripts, and I just can't seem to get it right...

TIA,

-d.
 
Old 01-17-2002, 09:14 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
if [ `program | grep -c "string"` -gt 0 ]
then
echo ok
else
echo not ok
fi

Last edited by Mik; 01-17-2002 at 09:20 AM.
 
Old 01-18-2002, 07:23 AM   #3
dguy
Member
 
Registered: Dec 2001
Location: Beckwith Township, Ontario, Canada
Distribution: Slackware 14.0, Slackware-64 14.1
Posts: 119

Original Poster
Rep: Reputation: 16
Unhappy

Ahh... it was the quotes around the command line which I was missing.

If you don't mind though, I could still use some help. :-)

After some debugging, I discovered that the expression was never evaluating to true. Echoing the statement to the command line produces a "no such file or directory" error, even when I fully qualify the program locations. e.g. "/sbin/route -n | /bin/grep -c "255.255.255.255".

Any ideas? The script is setting a path of /sbin:/usr/sbin:/bin:/usr/bin, so I would have assumed that this was sufficient.

Something's still missing though... :-|

-d.
 
Old 01-18-2002, 07:43 AM   #4
bluecadet
Member
 
Registered: Oct 2001
Distribution: MD81 RH71
Posts: 555

Rep: Reputation: 30
if [ `/sbin/route | grep -c "255"` -gt 0 ]
then
echo ok
else
echo not ok
fi

well that works for me... i guess it's your string your grepping with, 255.255.255.255 wouldn't normaly be set up. not sure wat it would do actually...
 
Old 01-19-2002, 08:00 AM   #5
dguy
Member
 
Registered: Dec 2001
Location: Beckwith Township, Ontario, Canada
Distribution: Slackware 14.0, Slackware-64 14.1
Posts: 119

Original Poster
Rep: Reputation: 16
Thumbs up Go it!

I found my problem:

> if [ `/sbin/route | grep -c "255"` -gt 0 ]

I was using the (') single qoute to wrap the command, instead of (`). I didn't realize that there was a difference...


Many thanks to all!

-d.
 
  


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
Bash scripting help (su ...) shwong Linux - General 1 11-02-2005 12:26 PM
Bash scripting pete1234 Programming 1 09-27-2005 01:48 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 12:50 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 01:10 AM

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

All times are GMT -5. The time now is 06:32 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