Is it possible to kill a loop in bash without killing the instance of bash running the loop?

5

1

Suppose I execute (in an interactive bash shell) a loop that spawns a bunch of processes in sequence, such as the following.

for i in `seq 15 42`; do if [ -f F$i.flv.mp3 ]; then play F$i.flv.mp3; fi; done 

If I kill the current process with Control-C, it will just go to the next process in the loop, and I have to kill the processes started by an iteration of the loop separately.

Without killing my interactive shell, can I stop the loop from generating or more processes in sequence?

merlin2011

Posted 2013-04-25T22:46:46.080

Reputation: 1 409

Answers

4

In my tests Control-C breaks the loop, but if it didn't, I'd do a Control-Z to stop the job, and then kill %1 to kill the whole job.

Ramillete

Posted 2013-04-25T22:46:46.080

Reputation: 61

0

In general no, but I've been known to add this to a long running loop:

if test -f /tmp/please-stop; then break; fi

PaulC

Posted 2013-04-25T22:46:46.080

Reputation: 11