View Full Version : [Solved] -n option for /bin/test
jrick
August 15th, 2009, 19:54
I am trying to convert a simple script that I wrote from zsh to /bin/sh, but it seems that the behavior of the -n option for conditional tests is different from zsh.
Example from zsh:
if [[ -n `pgrep xmms2d` ]]; then
echo "This code will not be run."
fi
And in sh:
if [ -n `pgrep xmms2d` ]; then
echo "This code will run."
fi
Can anyone explain this, and a possible work around that I can use?
DutchDaemon
August 15th, 2009, 20:01
Are you looking for something like this?
if pgrep xmms2d
then
echo "This code will run."
fi
or even
pgrep xmms2d && echo "This code will run."
pgrep xmms2d || echo "This code will not run."
jrick
August 15th, 2009, 20:14
Are you looking for something like this?
if ! pgrep xmms2d
or even
pgrep xmms2d || echo "This code will run."
Well, maybe it would be good if I showed the whole script:
#!/bin/sh
UPDATE=1
SYSTEM=`uname -sr`
while :; do
DATE=`date +"%a %B %d, %I:%M %p"`
BATTERY=`sysctl -n hw.acpi.battery.life`
# if xmms2d is running
if [ -n `pgrep xmms2d` ]; then
# another if statement, stopped or not
if [ `nyxmms2 status | grep -Eo '^Stopped:'` = "Stopped:" ]; then
XMMS2=" ^fg(green)XMMS2 is stopped^fg() |"
else
XMMS2=" ^fg(green)`nyxmms2 status | grep -Eo '^[^:]+:[^:]+'`^fg() |"
fi
else
XMMS2=""
fi
echo "^fg(red)$SYSTEM^fg() |$XMMS2 ^fg(#00FFFF)$BATTERY%^fg() | ^fg(orange)$DATE^fg() "
sleep $UPDATE
done | dzen2 -fn '-*-terminus-medium-r-*-*-12-*-*-*-*-*-*-*' -bg '#222222' -fg '#aaaaaa' -w 840 -ta r -x 840
I want to test to see if xmms2d is running, and if it is, then run the rest of that code.
This almost works:
if ! pgrep xmms2d ; then
echo "test"
fi
"test" is not echoed at all, but it still prints the pid of xmms2d, which I really don't want.
DutchDaemon
August 15th, 2009, 20:18
if ! pgrep xmms2d > /dev/null 2>&1 ; then
echo "test"
fi
jrick
August 15th, 2009, 20:21
Ah yes, such a simple solution. Thanks!
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.