Scenario:
I have a shell script running on embedded linux. The script starts an application which needs the state of a variable to be on.
Code:
So I do it like this
#!/bin/sh
start_my_app=false
wait_for_something=true
while $wait_for_something; do
wait_for_something=$(cat /some/path/file)
if [ "$wait_for_something" = "false" ]
then
echo Waiting...
elif [ "$wait_for_something" = "true" ]
then
echo The wait has ended
wait_for_something=false
start_my_app=true
else
fi
done
if [ "$start_my_app" = "true" ]
then
/usr/bin/MyApp
fi
#End of the script
/some/path/file
has a value false
and turns to true
in a few seconds by another script in different component. And then as the logic goes wait_for_something
in my script becomes true
and /usr/bin/MyApp
is started.
The problem and hence the question:
But I want to do it in a better way.
I dont want to wait infinitely in a while loop expecting the content value in /some/path/file
to be set true
after some time.
I want to wait for the content value in /some/path/file
to be set true
for only 5 seconds. If /some/path/file
does not contain true
in 5 seconds, I want to get out setting start_my_app
to false.
How can I achieve this functionality in a shell script on linux?
PS:
My whole script is run in the background by another script