2

Im tring to make a script to execute a set of commands from a file

the file for example has a set of 3 commands perl script-a, perl script-b, perl script-c, each command on a new line and i made this script

#!/bin/bash
for command in `cat file.txt`
do
   echo $command
   perl $command

done

The problem is that some scripts get stuck or takes too long to finish and i want to see their outputs. It is possible to make the bash script in case i send CTRL+C on the current command that is executed to jump to the next command in the txt file not to cancel the wole bash script.

Thank you

2 Answers 2

4

You can use trap 'continue' SIGINT to ignore Ctrl+c:

#!/bin/bash
# ignore & continue on Ctrl+c (SIGINT)
trap 'continue' SIGINT

while read command
do
   echo "$command"
   perl "$command"
done < file.txt

# Enable Ctrl+c
trap SIGINT

Also you don't need to call cat to read a file's contents.

Sign up to request clarification or add additional context in comments.

4 Comments

This isn't going to jump to the next command in the file when Ctrl+C is pressed.
@dogbane: Good point, thanks for pointing it out. I made a correction.
The first line of your answer says "SIGNINT" instead of "SIGINT" - I tried to edit it but I have to change at least 6 characters so I couldn't.
One question here if you don't use the cat command how do you read the file content then?
0
#!/bin/bash
for scr in $(cat file.txt)
do
 echo $scr

 # Only if you have a few lines in your file.txt,
 # Then, execute the perl command in the background
 # Save the output.
 # From your question it seems each of these scripts are independent

 perl $scr &> $scr_perl_execution.out &

done

You can check each of the output to see if the command is doing as you expect. If not, you can use kill to terminate each of the command.

2 Comments

This will background each process, not "forward" an interrupt.
It does, however, preclude the necessity of using Control-C to abort a stuck script to allow the next one to proceed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.