PDA

View Full Version : I need some help with a very simple bash file...



Yes
March 7th, 2008, 08:02 PM
I'm trying to write a program that will output the current state of mpd.

Here's what I have so far:


#!bin/sh

mpc_stopped=`exec mpc | grep stopped | cut -c 2-3`
mpc_paused=`exec mpc | grep paused | cut -c 2-3`
mpc_playing=`exec mpc | grep playing | cut -c 2-3`
echo $mpc_stopped
echo $mpc_paused
echo $mpc_playing

if [ "$mpc_stopped" == "st"]
then
echo "STOPPED"
elif ["$mpc_paused" == "pa"]
then
echo "PAUSED"
elif ["$mpc_playing" == "pl"]
then
echo "PLAYING"
fi

And here's the errors...





pl
[: 19: missing ]
[: 19: missing ]
mpc.sh: 19: [pl: not found

So I have no idea why it's doing that. Can anyone help?

olejorgen
March 7th, 2008, 08:09 PM
I think you need ; after if [ ..], like this if [ ... ];, also you need to separate the ] and [ from the test arguments with a space.



#!bin/sh

mpc_stopped=`exec mpc | grep stopped | cut -c 2-3`
mpc_paused=`exec mpc | grep paused | cut -c 2-3`
mpc_playing=`exec mpc | grep playing | cut -c 2-3`
echo $mpc_stopped
echo $mpc_paused
echo $mpc_playing

if [ "$mpc_stopped" == "st" ]; # [ and ] must be "surrounded" by spaces
then
echo "STOPPED"
elif [ "$mpc_paused" == "pa" ];
then
echo "PAUSED"
elif [ "$mpc_playing" == "pl" ];
then
echo "PLAYING"
fi

Yes
March 7th, 2008, 08:21 PM
Ok, thanks. But now I have a new error:


[: 19: ==: unexpected operator
[: 19: ==: unexpected operator
[: 19: ==: unexpected operator

You do use == to compare two strings, don't you?

WW
March 7th, 2008, 08:57 PM
Be sure you have spaces after [ and before ].

Yes
March 7th, 2008, 09:02 PM
Like


if [ "$mpc_stopped" == "$st" ];

right?

ghostdog74
March 8th, 2008, 05:14 AM
use case, neater. and i believe you only have 1 type of status output..



mpa_status=`mpc | egrep "stopped|paused|playing" | cut -c 2-3`
case $mpa_status in
"st" ) echo "stop";;
"pa") echo "pause";;
"pl" ) echo "play";;
esac

Whiffle
March 8th, 2008, 06:13 AM
#!bin/sh

should be

#!/bin/sh

olejorgen
March 8th, 2008, 07:53 AM
Either use only one "=", or use double brackets. ie. [[ ... ]] or [ ... = ... ]