I have store commands in file and i want to capture their output, but what is wrong here?
我在文件中存储命令,我想捕获它们的输出,但这里有什么问题?
I want to capture output of each command and store in relevant file.
我想捕获每个命令的输出并存储在相关文件中。
Following just example, i have 100s of command which i want to run and capture optput but i just stuck in my demo testig in following code.
仅举几个例子,我有100个命令,我想运行并捕获optput但我只是在下面的代码中插入我的演示testig。
My foo.txt
/bin/ls
/bin/cat /etc/redhat-release
My for loop
我的循环
[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
Why /bin/ls
run but /bin/cat /etc/redhat-release
not run
为什么/ bin / ls运行但是/ bin / cat / etc / redhat-release没有运行
UPDATE:
[spatel@linux ~]$ /bin/cat /etc/redhat-release
CentOS release 6.6 (Final)
4 个解决方案
#1
With your command as shown, neither ls
not cat
is actually executed:
使用如图所示的命令,实际上并不执行cat:
$ for qw in `cat foo.txt`; do echo $qw; done
/bin/ls
/bin/cat /etc/redhat-release
To get the output that you show, I suspect that you actually ran this command;
为了获得你显示的输出,我怀疑你实际运行了这个命令;
$ for qw in `cat foo.txt`; do $qw; done
foo.txt
bash: /bin/cat /etc/redhat-release: No such file or directory
In this case, ls
is executed but /bin/cat /etc/redhat-release
is not executed because there is no command whose name is the full string /bin/cat /etc/redhat-release
. (The shell does not do word-splitting here.)
在这种情况下,执行ls但不执行/ bin / cat / etc / redhat-release,因为没有名称为完整字符串/ bin / cat / etc / redhat-release的命令。 (shell不会在这里进行分词。)
To see this in a simpler example:
要在一个更简单的示例中看到这一点:
$ ls *.txt
foo.txt
$ a="ls *.txt"; $a
bash: ls *.txt: command not found
Executing all the commands and capturing output to file
To execute all the command in foo.txt, run:
要在foo.txt中执行所有命令,请运行:
$ bash foo.txt
foo.txt
CentOS release 6.6 (Final)
To run all the commands and capture their output to file output.log
, then run:
要运行所有命令并将其输出捕获到文件output.log,请运行:
bash foo.txt >output.log
To run all the commands and show their output on screen while also capturing the output to a file:
要运行所有命令并在屏幕上显示其输出,同时还将输出捕获到文件:
$ bash foo.txt | tee output.log
foo.txt
CentOS release 6.6 (Final)
Capturing each command's output to a different file
First, run awk on foo.txt
to create foo.sh
:
首先,在foo.txt上运行awk来创建foo.sh:
$ awk '{sub(/$/, (" >output" ++count))} 1' foo.txt >foo.sh
This is what foo.sh
looks like:
这就是foo.sh的样子:
$ cat foo.sh
/bin/ls >output1
/bin/cat /etc/redhat-release >output2
As you can see, each command in the file now has its stdout sent to a numbered file. If you want to capture stderr as well, that is just a small change.
如您所见,文件中的每个命令现在都将其stdout发送到编号文件。如果你想捕获stderr,那只是一个小小的改变。
Now, run foo.sh
:
现在,运行foo.sh:
$ bash foo.sh
$ cat output1
foo.sh
foo.txt
output1
#2
It happens obviously because the file /etc/redhat-release
does not exist.
它显然是因为文件/ etc / redhat-release不存在。
Btw, if you want to execute commands from a file use a shellscript!
顺便说一句,如果你想从文件执行命令使用一个shellscript!
source foo.sh # will execute the commands in the current shell
bash foo.sh # will launch a new shell to execute the commands
The file extension .sh
is not necessary to make it work. but it should being used by convention.
文件扩展名.sh不是必需的,以使其工作。但它应该按照惯例使用。
#3
What you're looking for is the eval command:
您正在寻找的是eval命令:
while read qw; do eval $qw; done < foo.txt
The use of eval is strongly frowned upon though, (as it tends to open up lots of security holes).
然而,使用eval是非常不受欢迎的(因为它往往会打开许多安全漏洞)。
Another option is to generate a new script file from the original, using sed to attach redirect tags to the end of each line.
另一种选择是从原始文件生成一个新的脚本文件,使用sed将重定向标记附加到每行的末尾。
#4
You have the commands you want in foo.txt.
How about:
您在foo.txt中拥有所需的命令。怎么样:
chmod +x foo.txt
./foo.txt > my_output
When this works fine, consider renaming foo.txt into foo.sh.
当这工作正常时,考虑将foo.txt重命名为foo.sh.
#1
With your command as shown, neither ls
not cat
is actually executed:
使用如图所示的命令,实际上并不执行cat:
$ for qw in `cat foo.txt`; do echo $qw; done
/bin/ls
/bin/cat /etc/redhat-release
To get the output that you show, I suspect that you actually ran this command;
为了获得你显示的输出,我怀疑你实际运行了这个命令;
$ for qw in `cat foo.txt`; do $qw; done
foo.txt
bash: /bin/cat /etc/redhat-release: No such file or directory
In this case, ls
is executed but /bin/cat /etc/redhat-release
is not executed because there is no command whose name is the full string /bin/cat /etc/redhat-release
. (The shell does not do word-splitting here.)
在这种情况下,执行ls但不执行/ bin / cat / etc / redhat-release,因为没有名称为完整字符串/ bin / cat / etc / redhat-release的命令。 (shell不会在这里进行分词。)
To see this in a simpler example:
要在一个更简单的示例中看到这一点:
$ ls *.txt
foo.txt
$ a="ls *.txt"; $a
bash: ls *.txt: command not found
Executing all the commands and capturing output to file
To execute all the command in foo.txt, run:
要在foo.txt中执行所有命令,请运行:
$ bash foo.txt
foo.txt
CentOS release 6.6 (Final)
To run all the commands and capture their output to file output.log
, then run:
要运行所有命令并将其输出捕获到文件output.log,请运行:
bash foo.txt >output.log
To run all the commands and show their output on screen while also capturing the output to a file:
要运行所有命令并在屏幕上显示其输出,同时还将输出捕获到文件:
$ bash foo.txt | tee output.log
foo.txt
CentOS release 6.6 (Final)
Capturing each command's output to a different file
First, run awk on foo.txt
to create foo.sh
:
首先,在foo.txt上运行awk来创建foo.sh:
$ awk '{sub(/$/, (" >output" ++count))} 1' foo.txt >foo.sh
This is what foo.sh
looks like:
这就是foo.sh的样子:
$ cat foo.sh
/bin/ls >output1
/bin/cat /etc/redhat-release >output2
As you can see, each command in the file now has its stdout sent to a numbered file. If you want to capture stderr as well, that is just a small change.
如您所见,文件中的每个命令现在都将其stdout发送到编号文件。如果你想捕获stderr,那只是一个小小的改变。
Now, run foo.sh
:
现在,运行foo.sh:
$ bash foo.sh
$ cat output1
foo.sh
foo.txt
output1
#2
It happens obviously because the file /etc/redhat-release
does not exist.
它显然是因为文件/ etc / redhat-release不存在。
Btw, if you want to execute commands from a file use a shellscript!
顺便说一句,如果你想从文件执行命令使用一个shellscript!
source foo.sh # will execute the commands in the current shell
bash foo.sh # will launch a new shell to execute the commands
The file extension .sh
is not necessary to make it work. but it should being used by convention.
文件扩展名.sh不是必需的,以使其工作。但它应该按照惯例使用。
#3
What you're looking for is the eval command:
您正在寻找的是eval命令:
while read qw; do eval $qw; done < foo.txt
The use of eval is strongly frowned upon though, (as it tends to open up lots of security holes).
然而,使用eval是非常不受欢迎的(因为它往往会打开许多安全漏洞)。
Another option is to generate a new script file from the original, using sed to attach redirect tags to the end of each line.
另一种选择是从原始文件生成一个新的脚本文件,使用sed将重定向标记附加到每行的末尾。
#4
You have the commands you want in foo.txt.
How about:
您在foo.txt中拥有所需的命令。怎么样:
chmod +x foo.txt
./foo.txt > my_output
When this works fine, consider renaming foo.txt into foo.sh.
当这工作正常时,考虑将foo.txt重命名为foo.sh.