LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 11-15-2011, 09:50 AM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 425
Blog Entries: 28

Rep: Reputation: 2
pgrep "program" and if statement


Hi, I don't know why this always returns the false boolean:
Code:
if [ ! -z $(pgrep "mediatomb") ] ; then
			mtstatus="Mediatomb is running!"
		else
			mtstatus="Mediatomb is not running!"
		fi
I tried:
Code:
echo $(pgrep "mediatomb")
1222 1223 1224 1225 1230 1232 1234 1236 1237 1238 1975 1979 1980 1981
Thanks,
Ted
 
Old 11-15-2011, 10:04 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
The command substitution is performed before the test take places, so that it actually is something like:
Code:
if [ ! -z 1222 1223 1224 1225 1230 1232 1234 1236 1237 1238 1975 1979 1980 1981 ] ; then
This should throw an error for too many arguments or binary operator expected. Anyway, what you really need is
Code:
if pgrep bash; then
that checks the exit status of the pgrep command: when processes are found the exit status is 0 (success) and the test is true, when processes don't exist, the exit status is 1 (failure) and the test is false. Anyway, I cannot explain why your test doesn't throw an error message and returns always false.

Moreover note that ! -z is the same as -n (the length of string is nonzero).
 
Old 11-15-2011, 12:04 PM   #3
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 425
Blog Entries: 28

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by colucix View Post
The command substitution is performed before the test take places, so that it actually is something like:
Code:
if [ ! -z 1222 1223 1224 1225 1230 1232 1234 1236 1237 1238 1975 1979 1980 1981 ] ; then
This should throw an error for too many arguments or binary operator expected. Anyway, what you really need is
Code:
if pgrep bash; then
that checks the exit status of the pgrep command: when processes are found the exit status is 0 (success) and the test is true, when processes don't exist, the exit status is 1 (failure) and the test is false. Anyway, I cannot explain why your test doesn't throw an error message and returns always false.

Moreover note that ! -z is the same as -n (the length of string is nonzero).

Thanks for reminding, I realized that double quote should be made:
Code:
if [ ! -z "$(pgrep "mediatomb")" ] ; then
			mtstatus="Mediatomb is running!"
		else
			mtstatus="Mediatomb is not running!"
		fi
 
  


Reply


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
swatch: Multiple fields in "threshold track_by" statement kenneho Linux - Server 0 06-09-2011 05:19 AM
[SOLVED] A confusing statement about "locking an account" in the ClamAV documentation. pr_deltoid Linux - Software 3 06-24-2010 04:09 AM
Program to "assemble" still-frame pictures into "animated" show Pantherman Linux - General 3 12-24-2009 11:14 PM
hard drives come up "no partition table" and "in use by another program" marti972 Linux - Newbie 3 03-16-2009 11:44 PM
Getting error: "ALSA device "default" is already in use by another program." brynjarh Debian 7 02-04-2005 11:45 AM

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

All times are GMT -5. The time now is 07:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration