Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
Distribution: Slackware 11.0; Kubuntu 6.06; OpenBSD 4.0; OS X 10.4.10
Posts: 345
Rep:
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:
then
echo $?
echo already run
else
echo $?
echo does not exist
fi
My bet is that your output will always be:
15404 pts/3 00:00:00 jackd
1
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.