You are not logged in.

#1 2008-09-09 16:55:05

gazj
Member
From: /home/gazj -> /uk/cambs
Registered: 2007-02-09
Posts: 681
Website

Help with a bash script, please :)

My bash is not good, not good at all, I can't work out why this will not work.  Any help is appreciated.

[gary@Lister ~]$ cat .bin/kiss 
#!/bin/bash
if [ -t /tmp/kiss ]
    killall mplayer
else
    touch /tmp/kiss
    mplayer -playlist http://www.emapdigitalradio.com/emapdigitalradio/metafiles/kiss105.asx
fi

errors are

[gary@Lister ~]$ kiss
/home/gary/.bin/kiss: line 4: syntax error near unexpected token `else'
/home/gary/.bin/kiss: line 4: `else'

Thanks guys

Offline

#2 2008-09-09 17:14:18

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Help with a bash script, please :)

if [ -t /tmp/kiss ] ; then

Offline

#3 2008-09-09 19:22:58

mazzarelli
Member
Registered: 2008-08-27
Posts: 6

Re: Help with a bash script, please :)

#!/bin/bash
CMD='mplayer -playlist http://www.emapdigitalradio.com/emapdigitalradio/metafiles/kiss105.asx'

ps u -C mplayer | grep "$CMD" >/dev/null
if [ $? = 0 ]; then
  killall mplayer 
else
  $CMD &>/dev/null &
  disown %1
fi

(assuming you wanted this script to toggle whether or not the program was playing)

This should allow you to run "kiss" then close the shell. It will still play.

Last edited by mazzarelli (2008-09-09 19:24:21)

Offline

#4 2008-09-10 21:02:42

gazj
Member
From: /home/gazj -> /uk/cambs
Registered: 2007-02-09
Posts: 681
Website

Re: Help with a bash script, please :)

Sorry the reply is so late.  Work smile

Thank you Dusty that did help loads.  I knew someone would come back with a hughely superior script.  Thanks mazzarelli.

Care to explain how it works for someone who is a dab hand at c64 basic but not a lot a good at much else. smile

Thanks

Gary

Offline

#5 2008-09-10 23:04:41

Arkane
Member
From: Switzerland
Registered: 2008-02-18
Posts: 263

Re: Help with a bash script, please :)

Not 100% sure about it, but:

ps u -C mplayer | grep "$CMD" >/dev/null

ps lists all the user's processes run with the "mplayer" command, and grep tries to find the right instance of mplayer in that list. It doesn't actually do anything, but read what comes next:

if [ $? = 0 ]; then
  killall mplayer 
else
  $CMD &>/dev/null &

the $? variable contains the exit status of the last command; it tells us whether the grep above succeeded. If it did, then the program is running, it will be stopped. If not, it is started (with all its output redirected to /dev/null).


  disown %1
fi

I didn't know about disown before, but if I understood things correctly it will make the program we just started independent of the shell running the script, so that it keeps running when the shell exits.


What does not kill you will hurt a lot.

Offline

#6 2008-09-12 18:12:40

mazzarelli
Member
Registered: 2008-08-27
Posts: 6

Re: Help with a bash script, please :)

Arkane wrote:

I didn't know about disown before, but if I understood things correctly it will make the program we just started independent of the shell running the script, so that it keeps running when the shell exits.

Yeah, the shell keeps track of its jobs, and when the shell exits, it sends a SIGHUP signal to its jobs. The "disown" command tells the shell not to bother (and stop keeping track of this job), allowing you to close the shell without disturbing the running jobs.

You can get this effect other ways also.

Here is a better version of the script:

#!/bin/bash
CMD='mplayer -playlist http://www.emapdigitalradio.com/emapdigitalradio/metafiles/kiss105.asx'

MPID=$(ps u -C mplayer | grep "$CMD" | awk '{print $2}')
if [ -z "$MPID" ]; then
  #echo -n starting kiss...
  $CMD &>/dev/null &
  disown %1
  #echo done
else
  #echo -n stopping kiss...
  kill $MPID
  #echo done
fi

This one is less ambitious in killing all instances of mplayer. Uncomment the "echo" lines if you want some feedback.

Offline

Board footer

Powered by FluxBB