简体   繁体   English

Bash脚本从文件执行命令,但如果取消则跳到下一个

[英]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 例如,该文件有一组3个命令,分别是perl脚本-a,perl脚本-b,perl脚本-c,每个命令都换行,而我制作了此脚本

#!/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. 如果我在执行的当前命令上发送CTRL + C以跳转到txt文件中的下一个命令而不取消wole bash脚本的情况下,可以制作bash脚本。

Thank you 谢谢

You can use trap 'continue' SIGINT to ignore Ctrl+c : 您可以使用trap 'continue' SIGINT来忽略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. 另外,您无需调用cat即可读取文件的内容。

#!/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. 如果不是,则可以使用kill终止每个命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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