簡體   English   中英

Bash腳本從文件執行命令,但如果取消則跳到下一個

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

制作腳本以執行文件中的一組命令

例如,該文件有一組3個命令,分別是perl腳本-a,perl腳本-b,perl腳本-c,每個命令都換行,而我制作了此腳本

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

done

問題是某些腳本卡住或完成時間太長,我想查看它們的輸出。 如果我在執行的當前命令上發送CTRL + C以跳轉到txt文件中的下一個命令而不取消wole bash腳本的情況下,可以制作bash腳本。

謝謝

您可以使用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

另外,您無需調用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

您可以檢查每個輸出,以查看命令是否按預期執行。 如果不是,則可以使用kill終止每個命令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM