General TC > General TC Talk

scripting q-- wait for response?

(1/6) > >>

mb0:
How would i delay/sleep a bash script to wait until a command has given a return? For example, the 'mpc idle' command only returns when something changes, driven by external events. So i don't think it works the same way as waiting for a program to complete.

gerald_clark:
That is the default behavior.
If you want a script to start a program and NOT wait for it to terminate, you must use '&' to run it in background.

mb0:
ok, well this is my script;


--- Code: ---while true
do
        read line < /usr/local/bin/mpc idle
        if [ $line == "player" ]; then
                sudo echo 'not'
        else
                sudo echo 'idle'
        fi
        sleep 0.2
done

--- End code ---

and it echo's 'idle' every .2 seconds. (NB without else or sleep it doesn't do anything, even with external events firing)
in case you are not familiar; 'mpc idle' will return 'player' when an external event changes something with mpd. Until that, it doesn't return anything.

gerald_clark:
This really is not a bash forum, but

read line < /usr/local/bin/mpc idle
tries to read from the file /usr/local/bin/mpc

Try
line=`/usr/local/bin/mpc idle`
or
line=$( /usr/local/bin/mpc idle )

mb0:
Well thanks for replying anyway. I would urge a general linux sub-forum to filter out these questions from those who don't want to see them.

I have a slightly more tinycore related follow up.. if i try and run this script in eg. bootlocal.sh, it doesn't work. However if i run it after, it does. How can i get it to work automatically on boot?

Navigation

[0] Message Index

[#] Next page

Go to full version