WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: scripting q-- wait for response?  (Read 8040 times)

Offline mb0

  • Jr. Member
  • **
  • Posts: 86
scripting q-- wait for response?
« on: February 07, 2012, 06:06:34 PM »
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.

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: scripting q-- wait for response?
« Reply #1 on: February 07, 2012, 06:33:30 PM »
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.

Offline mb0

  • Jr. Member
  • **
  • Posts: 86
Re: scripting q-- wait for response?
« Reply #2 on: February 07, 2012, 07:16:58 PM »
ok, well this is my script;

Code: [Select]
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

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.

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: scripting q-- wait for response?
« Reply #3 on: February 07, 2012, 07:52:21 PM »
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 )

Offline mb0

  • Jr. Member
  • **
  • Posts: 86
Re: scripting q-- wait for response?
« Reply #4 on: February 11, 2012, 02:28:18 PM »
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?

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: scripting q-- wait for response?
« Reply #5 on: February 11, 2012, 02:46:21 PM »
You should not run anything in bootlocal.sh that waits for a response unless a response is assured.

Offline mb0

  • Jr. Member
  • **
  • Posts: 86
Re: scripting q-- wait for response?
« Reply #6 on: February 11, 2012, 03:53:40 PM »
so what is the proper method to auto run this on boot?

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: scripting q-- wait for response?
« Reply #7 on: February 11, 2012, 03:59:46 PM »
Why would you want to?

Offline mb0

  • Jr. Member
  • **
  • Posts: 86
Re: scripting q-- wait for response?
« Reply #8 on: February 11, 2012, 04:35:17 PM »
because it is meant to be headless. And I still don't understand why this doesn't work by saving it as a script and then calling '/opt/path/script.sh &' ?

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: scripting q-- wait for response?
« Reply #9 on: February 11, 2012, 04:39:51 PM »
What does it accomplish?

Offline mb0

  • Jr. Member
  • **
  • Posts: 86
Re: scripting q-- wait for response?
« Reply #10 on: February 11, 2012, 04:59:00 PM »
I'm  not quite sure why it matters... i'm sure i need it.. but anyway; To provide an update when there is an event from mpc. For example, to update an LCD.

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: scripting q-- wait for response?
« Reply #11 on: February 11, 2012, 05:24:57 PM »
The script you showed above does nothing but echo to console.

Offline mb0

  • Jr. Member
  • **
  • Posts: 86
Re: scripting q-- wait for response?
« Reply #12 on: February 11, 2012, 05:34:55 PM »
Yes i'm aware of that, it was a non complete example. Obviously i replace the echo with other code. Like i said, it works when i launch it myself. Now, like i asked, how do i get it to run automatically on boot?

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
Re: scripting q-- wait for response?
« Reply #13 on: February 11, 2012, 05:51:26 PM »
Why don't you make an extension of the script, then add a file to X.d which starts the script on each boot?

I think instructions are in the wiki or Q&A section..

Offline mb0

  • Jr. Member
  • **
  • Posts: 86
Re: scripting q-- wait for response?
« Reply #14 on: February 11, 2012, 06:02:56 PM »
Do i need to create an extension?? The script is persistant in /opt. I guess i would be using init.d instead of x.d since i am not using the gui ??

I would still also like to know *why* it doesn't work just running 'script.sh &' style from another script like bootlocal.sh ?