for loop 运行 来自文件的命令
for loop run command in from file
我在文件中有存储命令,我想捕获它们的输出,但是这里有什么问题吗?
我想捕获每个命令的输出并存储在相关文件中。
仅举个例子,我有 100 条命令,我想 运行 并捕获 optput,但我只是在下面的代码中停留在我的演示测试中。
我的foo.txt
/bin/ls
/bin/cat /etc/redhat-release
我的for循环
[spatel@linux ~]$ IFS=$'\n'
[spatel@linux ~]$ for qw in `cat foo.txt`; do echo $qw; done
backup bar final.sh foo.txt input.txt
-bash: /bin/cat /etc/redhat-release: No such file or directory
为什么 /bin/ls
运行 但 /bin/cat /etc/redhat-release
不是 运行
更新:
[spatel@linux ~]$ /bin/cat /etc/redhat-release
CentOS release 6.6 (Final)
显然是因为文件 /etc/redhat-release
不存在。
顺便说一句,如果你想从文件中执行命令使用 shellscript!
source foo.sh # will execute the commands in the current shell
bash foo.sh # will launch a new shell to execute the commands
文件扩展名 .sh
不是使其工作所必需的。但它应该按照惯例使用。
如图所示,ls
和 cat
都没有实际执行:
$ for qw in `cat foo.txt`; do echo $qw; done
/bin/ls
/bin/cat /etc/redhat-release
为了得到你显示的输出,我怀疑你实际上 运行 这个命令;
$ for qw in `cat foo.txt`; do $qw; done
foo.txt
bash: /bin/cat /etc/redhat-release: No such file or directory
在这种情况下,执行了ls
,但没有执行/bin/cat /etc/redhat-release
,因为没有名称为完整字符串/bin/cat /etc/redhat-release
的命令。 (shell 这里不做分词。)
要在更简单的示例中查看:
$ ls *.txt
foo.txt
$ a="ls *.txt"; $a
bash: ls *.txt: command not found
正在执行所有命令并将输出捕获到文件
执行foo.txt中的所有命令,运行:
$ bash foo.txt
foo.txt
CentOS release 6.6 (Final)
到运行所有命令并将它们的输出捕获到文件output.log
,然后运行:
bash foo.txt >output.log
到 运行 所有命令并在屏幕上显示它们的输出,同时还将输出捕获到文件中:
$ bash foo.txt | tee output.log
foo.txt
CentOS release 6.6 (Final)
将每个命令的输出捕获到不同的文件
首先,运行 awk on foo.txt
创建 foo.sh
:
$ awk '{sub(/$/, (" >output" ++count))} 1' foo.txt >foo.sh
这就是 foo.sh
的样子:
$ cat foo.sh
/bin/ls >output1
/bin/cat /etc/redhat-release >output2
如您所见,文件中的每个命令现在都将其标准输出发送到编号文件。如果您也想捕获 stderr,那只是一个小改动。
现在,运行 foo.sh
:
$ bash foo.sh
$ cat output1
foo.sh
foo.txt
output1
您要查找的是 eval 命令:
while read qw; do eval $qw; done < foo.txt
虽然 eval 的使用受到强烈反对,(因为它往往会打开很多安全漏洞)。
另一种选择是从原始文件生成一个新的脚本文件,使用 sed 将重定向标签附加到每行的末尾。
您在 foo.txt 中拥有所需的命令。
怎么样:
chmod +x foo.txt
./foo.txt > my_output
当这工作正常时,考虑将 foo.txt 重命名为 foo.sh。
我在文件中有存储命令,我想捕获它们的输出,但是这里有什么问题吗?
我想捕获每个命令的输出并存储在相关文件中。
仅举个例子,我有 100 条命令,我想 运行 并捕获 optput,但我只是在下面的代码中停留在我的演示测试中。
我的foo.txt
/bin/ls
/bin/cat /etc/redhat-release
我的for循环
[spatel@linux ~]$ IFS=$'\n'
[spatel@linux ~]$ for qw in `cat foo.txt`; do echo $qw; done
backup bar final.sh foo.txt input.txt
-bash: /bin/cat /etc/redhat-release: No such file or directory
为什么 /bin/ls
运行 但 /bin/cat /etc/redhat-release
不是 运行
更新:
[spatel@linux ~]$ /bin/cat /etc/redhat-release
CentOS release 6.6 (Final)
显然是因为文件 /etc/redhat-release
不存在。
顺便说一句,如果你想从文件中执行命令使用 shellscript!
source foo.sh # will execute the commands in the current shell
bash foo.sh # will launch a new shell to execute the commands
文件扩展名 .sh
不是使其工作所必需的。但它应该按照惯例使用。
如图所示,ls
和 cat
都没有实际执行:
$ for qw in `cat foo.txt`; do echo $qw; done
/bin/ls
/bin/cat /etc/redhat-release
为了得到你显示的输出,我怀疑你实际上 运行 这个命令;
$ for qw in `cat foo.txt`; do $qw; done
foo.txt
bash: /bin/cat /etc/redhat-release: No such file or directory
在这种情况下,执行了ls
,但没有执行/bin/cat /etc/redhat-release
,因为没有名称为完整字符串/bin/cat /etc/redhat-release
的命令。 (shell 这里不做分词。)
要在更简单的示例中查看:
$ ls *.txt
foo.txt
$ a="ls *.txt"; $a
bash: ls *.txt: command not found
正在执行所有命令并将输出捕获到文件
执行foo.txt中的所有命令,运行:
$ bash foo.txt
foo.txt
CentOS release 6.6 (Final)
到运行所有命令并将它们的输出捕获到文件output.log
,然后运行:
bash foo.txt >output.log
到 运行 所有命令并在屏幕上显示它们的输出,同时还将输出捕获到文件中:
$ bash foo.txt | tee output.log
foo.txt
CentOS release 6.6 (Final)
将每个命令的输出捕获到不同的文件
首先,运行 awk on foo.txt
创建 foo.sh
:
$ awk '{sub(/$/, (" >output" ++count))} 1' foo.txt >foo.sh
这就是 foo.sh
的样子:
$ cat foo.sh
/bin/ls >output1
/bin/cat /etc/redhat-release >output2
如您所见,文件中的每个命令现在都将其标准输出发送到编号文件。如果您也想捕获 stderr,那只是一个小改动。
现在,运行 foo.sh
:
$ bash foo.sh
$ cat output1
foo.sh
foo.txt
output1
您要查找的是 eval 命令:
while read qw; do eval $qw; done < foo.txt
虽然 eval 的使用受到强烈反对,(因为它往往会打开很多安全漏洞)。
另一种选择是从原始文件生成一个新的脚本文件,使用 sed 将重定向标签附加到每行的末尾。
您在 foo.txt 中拥有所需的命令。
怎么样:
chmod +x foo.txt
./foo.txt > my_output
当这工作正常时,考虑将 foo.txt 重命名为 foo.sh。