[英]shell script - run list of commands
for i in `cat foo.txt`
do
$i
done
And I have a input file "foo.txt", with list of commands.我有一个输入文件“foo.txt”,其中包含命令列表。
ls -ltr | tail
ps -ef | tail
mysql -e STATUS | grep "^Uptime"
when I run the shell script, it executes, but splits the commands in each line at spaces ie for first line it executes only "ls", then "-ltr" for which I get command not found error.当我运行 shell 脚本时,它会执行,但将每行中的命令以空格分开,即对于第一行,它只执行“ls”,然后是“-ltr”,我得到命令未找到错误。
How can I run each list as one command?如何将每个列表作为一个命令运行?
I execute lot of arbitrary shell commands including DB commands.我执行了很多任意的 shell 命令,包括 DB 命令。 I need to have a error handling as I execute each command(each line from foo.txt), I can't think of what can go wrong, so the idea is put all commands in order and call them in loop and check for error (#?) at each line and stop on error.我需要在执行每个命令(来自 foo.txt 的每一行)时进行错误处理,我想不出会出什么问题,所以这个想法是将所有命令按顺序排列并在循环中调用它们并检查错误(#?) 在每一行,并在出错时停止。
Why not just do this?为什么不这样做呢?
set -e
. ./foo.txt
set -e
causes the shell script to abort if a command exits with a non-zero exit code, and . ./foo.txt
如果命令以非零退出代码退出,则set -e
会导致 shell 脚本中止,并且. ./foo.txt
. ./foo.txt
executes commands from foo.txt
in the current shell. . ./foo.txt
在当前 shell 中执行来自foo.txt
命令。
but I guess I can't send notification (email).但我想我无法发送通知(电子邮件)。
Sure you can.你当然可以。 Just run the script in a subshell, and then respond to the result code:只需在子shell中运行脚本,然后响应结果代码:
#!/bin/sh
(
set -e
. ./foo.txt
)
if [ "$?" -ne 0 ]; then
echo "The world is on fire!" | mail -s 'Doom is upon us' you@youremail.com
fi
Code mentioned.代码提到。
for i in `cat foo.txt`
do
$i
done
Please use https://www.shellcheck.net/请使用https://www.shellcheck.net/
This will result _
$ shellcheck myscript
Line 1:
for i in `cat foo.txt`
^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang.
^-- SC2013: To read lines rather than words, pipe/redirect to a 'while read' loop.
^-- SC2006: Use $(...) notation instead of legacy backticked `...`.
Did you mean: (apply this, apply all SC2006)
for i in $(cat foo.txt)
$
Will try while loop, and for test purpose content of foo.txt mentioned below将尝试 while 循环,为了测试目的,下面提到的 foo.txt 内容
cat foo.txt
ls -l /tmp/test
ABC
pwd
while read -r line; do $line; if [ "$?" -ne 0 ]; then echo "Send email Notification stating $line Command reported error "; fi; done < foo.txt
total 0
-rw-r--r--. 1 root root 0 Dec 24 11:41 test.txt
bash: ABC: command not found...
Send email Notification stating ABC Command reported error
/tmp
In case error reported you can break the loop.如果报告错误,您可以中断循环。 http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_05.html http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_05.html
while read -r line; do $line; if [ "$?" -ne 0 ]; then echo "Send email Notification stating $line Command reported error "; break; fi; done < foo.txt
total 0
-rw-r--r--. 1 root root 0 Dec 24 11:41 test.txt
bash: ABC: command not found...
Send email Notification stating ABC Command reported error
while read -r line; do eval $line; if [ "$?" -ne 0 ]; then echo "Send email Notification stating $line Command reported error "; break; fi; done < foo.txt
total 0
-rw-r--r--. 1 root root 0 Dec 24 11:41 test.txt
bash: ABC: command not found...
Send email Notification stating ABC Command reported error
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.