Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-31-2005, 03:14 AM   #1
Registered: Jan 2005
Distribution: Mandrake 10.1
Posts: 47

Rep: Reputation: 15
scripts - test with if


I was reading the Advanced Bash scripting HOWTO
and i tried to do a test script:
ps -A | grep "jackd"
#echo $?
if [ ! $? ]
   echo already run
   echo does not exist
and its output is
15404 pts/3 00:00:00 jackd
does not exist

Could someon explain me where is the catch???

Old 05-31-2005, 06:41 AM   #2
Registered: Sep 2004
Location: Des Moines, Iowa
Distribution: Slackware, Mandriva, Debian derivatives, +BSD/ Solaris/ Minix/ plan9/ GNU/HURD...
Posts: 185

Rep: Reputation: 30
um, I have no idea what output you were expecting.
You have the third line commented out. Remove the "#" ?
$? is a test of the return value of the last operation. It's a boolean 1 or 0.
If grep succeeds, it returns 0, if it doesn't find the expression, it returns 1.
Also, I usually enclose all test variables in double quotes:

if [ "$?" = "1" ]; then

To avoid confusion (if the variable you're testing for doesn't exist, you'll get errors. The double quotes is the "safe" way to test).

And finally, you can pipe grep's output to /dev/null, so it doesn't clutter up your output, unless you want it there.
Old 05-31-2005, 02:13 PM   #3
Registered: Oct 2003
Location: Heidelberg, Germany
Distribution: Slackware 11.0; Kubuntu 6.06; OpenBSD 4.0; OS X 10.4.10
Posts: 345

Rep: Reputation: 30
I was kind of playing around with this this evening, and I think the problem is that the value of $? in your if-then-else-fi statement is the return value of the test [ ! $? ]. Since $? has no hope of not being itself (your test statement says basically "if not $?"), the result of the test is 1 and therefore the echo command prints the statement "does not exist."

Try this. Put an echo $? statement inside the then and the else statements like this:

echo $?
echo already run
echo $?
echo does not exist

My bet is that your output will always be:

15404 pts/3 00:00:00 jackd
does not exist

Better yet, uncomment the echo $? statement you have immediately after the ps | grep statement, and see what you get.

The problem is, every time you do a ps | grep, or an echo, or a [ test ], you will reset $?.

Let me know if I'm wrong. I am still an advanced beginner scripter, and this problem and others I have seen here are interesting and a good learning experience.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
VPN: Debian Scripts -> Mandriva 2006 Scripts Undefined Mandriva 0 11-30-2005 12:10 PM
FC4 Test 1 CD Test failes every DISC Jimbo99 Linux - Software 1 03-18-2005 03:16 PM
test greenjanek Linux - Hardware 1 10-17-2004 07:27 AM
test hopbalt Linux - Newbie 2 09-19-2004 02:01 PM
Need help running scripts from scripts sdouble Linux - Newbie 3 05-31-2004 12:56 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:39 AM.

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