简体   繁体   中英

Bash Script execute commands from a file but if cancel on to jump on next one

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

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.

#!/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM