You are not logged in.
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
if [ -t /tmp/kiss ] ; then
Offline
#!/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
Sorry the reply is so late. Work
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.
Thanks
Gary
Offline
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
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