如何在Linux上传输命令的输出文件

时间:2021-11-01 14:04:55

I am running a task on the CLI, which prompts me for a yes/no input.

我正在CLI上运行一个任务,它提示我输入yes/no。

After selecting a choice, a large amount of info scrolls by on the screen - including several errors. I want to pipe this output to a file so I can see the errors. A simple '>' is not working since the command expects keyboard input.

在选择一个选项之后,大量的信息在屏幕上滚动——包括几个错误。我想把这个输出输出到一个文件中,这样我就可以看到错误了。简单的“>”不能工作,因为命令要求输入键盘。

I am running on Ubuntu 9.1.

我正在运行Ubuntu 9.1。

5 个解决方案

#1


38  

command &> output.txt

You can use &> to redirect both stdout and stderr to a file. This is shorthand for command > output.txt 2>&1 where the 2>&1 means "send stderr to the same place as stdout" (stdout is file descriptor 1, stderr is 2).

您可以使用&>将stdout和stderr重定向到一个文件。这是命令>输出的简写。txt 2>和1,其中2>和1表示“将stderr发送到与stdout相同的位置”(stdout是文件描述符1,stderr是2)。

For interactive commands I usually don't bother saving to a file if I can use less and read the results right away:

对于交互式命令,如果我可以使用更少的命令并立即读取结果,我通常不会麻烦地保存到文件中:

command 2>&1 | less

#2


4  

echo yes | command > output.txt

Depending on how the command reads it's input (some programs discard whatever was on stdin before it displays it's prompt, but most don't), this should work on any sane CLI-environment.

根据命令读取输入的方式(有些程序会在stdin显示提示符之前丢弃它上的内容,但大多数不会),这应该适用于任何正常的clic环境。

#3


1  

Use 2> rather than just >.

使用2>而不是>。

#4


1  

If the program was written by a sane person what you probably want is the stderr not the stdout. You would achieve this by using something like

如果程序是由一个理智的人编写的,你可能想要的是stderr而不是stdout。您可以通过使用类似的东西来实现这一点

foo 2> errors.txt

foo 2 > errors.txt

#5


1  

you can use 2> option to send errors to the file.

您可以使用2>选项将错误发送到文件。

example:

例子:

command 2> error.txt

命令2 > error.txt

(use of option 2>) --- see if their would be any error while the execution of the command it will send it to the file error.txt.

(使用选项2>)——在执行命令时查看它们是否有错误,并将其发送到file error.txt。

#1


38  

command &> output.txt

You can use &> to redirect both stdout and stderr to a file. This is shorthand for command > output.txt 2>&1 where the 2>&1 means "send stderr to the same place as stdout" (stdout is file descriptor 1, stderr is 2).

您可以使用&>将stdout和stderr重定向到一个文件。这是命令>输出的简写。txt 2>和1,其中2>和1表示“将stderr发送到与stdout相同的位置”(stdout是文件描述符1,stderr是2)。

For interactive commands I usually don't bother saving to a file if I can use less and read the results right away:

对于交互式命令,如果我可以使用更少的命令并立即读取结果,我通常不会麻烦地保存到文件中:

command 2>&1 | less

#2


4  

echo yes | command > output.txt

Depending on how the command reads it's input (some programs discard whatever was on stdin before it displays it's prompt, but most don't), this should work on any sane CLI-environment.

根据命令读取输入的方式(有些程序会在stdin显示提示符之前丢弃它上的内容,但大多数不会),这应该适用于任何正常的clic环境。

#3


1  

Use 2> rather than just >.

使用2>而不是>。

#4


1  

If the program was written by a sane person what you probably want is the stderr not the stdout. You would achieve this by using something like

如果程序是由一个理智的人编写的,你可能想要的是stderr而不是stdout。您可以通过使用类似的东西来实现这一点

foo 2> errors.txt

foo 2 > errors.txt

#5


1  

you can use 2> option to send errors to the file.

您可以使用2>选项将错误发送到文件。

example:

例子:

command 2> error.txt

命令2 > error.txt

(use of option 2>) --- see if their would be any error while the execution of the command it will send it to the file error.txt.

(使用选项2>)——在执行命令时查看它们是否有错误,并将其发送到file error.txt。