# -n option for /bin/test



## jrick (Aug 15, 2009)

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 (Aug 15, 2009)

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 (Aug 15, 2009)

DutchDaemon said:
			
		

> Are you looking for something like this?
> 
> 
> ```
> ...



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 (Aug 15, 2009)

```
if ! pgrep xmms2d > /dev/null 2>&1 ; then
  echo "test"
fi
```


----------



## jrick (Aug 15, 2009)

Ah yes, such a simple solution. Thanks!


----------

