Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Bash and external loop control
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Naib
Watchman
Watchman


Joined: 21 May 2004
Posts: 6051
Location: Removed by Neddy

PostPosted: Mon Mar 30, 2015 9:03 pm    Post subject: Bash and external loop control Reply with quote

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
View user's profile Send private message
frostschutz
Advocate
Advocate


Joined: 22 Feb 2005
Posts: 2977
Location: Germany

PostPosted: Mon Mar 30, 2015 9:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
Naib
Watchman
Watchman


Joined: 21 May 2004
Posts: 6051
Location: Removed by Neddy

PostPosted: Mon Mar 30, 2015 9:16 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum