View previous topic :: View next topic |
Author |
Message |
Naib Watchman
Joined: 21 May 2004 Posts: 6051 Location: Removed by Neddy
|
Posted: Mon Mar 30, 2015 9:03 pm Post subject: Bash and external loop control |
|
|
Say I wanted a while loop in bash, could I exit the loop from another bash instance?
I was thinking about using mkfifo
Code: |
mkfifo /tmp/fifo
while true
do
if read line /tmp/fifo; then
if [[ "$line" == 'quit' ]]; then
break
fi
echo $line
fi
done
|
This obviously waits for each echo "test" > /tmp/fifo and finally quits on echo "quit" > /tmp/fifo but what if I wanted to keep the loop running and wait for some external (to the process or the subprocess which is the while loop) to quit the loop? _________________
Quote: | Removed by Chiitoo |
|
|
Back to top |
|
|
frostschutz Advocate
Joined: 22 Feb 2005 Posts: 2977 Location: Germany
|
Posted: Mon Mar 30, 2015 9:09 pm Post subject: |
|
|
You could use 'trap' and then do something on kill.
Or just write quit to the fifo as it were... anyone can write quit, right? |
|
Back to top |
|
|
Naib Watchman
Joined: 21 May 2004 Posts: 6051 Location: Removed by Neddy
|
Posted: Mon Mar 30, 2015 9:16 pm Post subject: |
|
|
Just been looking into trap, that might be a cleaner method
The problem with the fifo is it blocks on the read so it doesn't allow the loop to essentially run async to any incoming signal - thus trap/kill might be better _________________
Quote: | Removed by Chiitoo |
|
|
Back to top |
|
|
|