Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在bash脚本中将所有命令输出记录到一个文件中_Bash - Fatal编程技术网

如何在bash脚本中将所有命令输出记录到一个文件中

如何在bash脚本中将所有命令输出记录到一个文件中,bash,Bash,在gnu/Linux中,我希望将所有命令输出记录到一个特定的文件中。 说在终端,我在打字 echo“嗨,这是个花花公子” 它应该以前面指定的文件名打印,而不必在每个命令中使用重定向 $ script x1 Script started, file is x1 $ echo "Hi this is a dude" Hi this is a dude $ echo "done" done $ exit exit Script done, file is x1 然后,文件x1的内容是: Script

在gnu/Linux中,我希望将所有命令输出记录到一个特定的文件中。 说在终端,我在打字

echo“嗨,这是个花花公子”

它应该以前面指定的文件名打印,而不必在每个命令中使用重定向

$ script x1
Script started, file is x1
$ echo "Hi this is a dude"
Hi this is a dude
$ echo "done"
done
$ exit
exit
Script done, file is x1
然后,文件x1的内容是:

Script started on Thu Jun 13 14:51:29 2013
$ echo "Hi this is a dude"
Hi this is a dude
$ echo "done"
done
$ exit
exit

Script done on Thu Jun 13 14:51:52 2013
您可以使用基本的shell脚本(
grep-v
)轻松编辑自己的命令和开始/结束行,尤其是在Unix提示符具有独特的子字符串模式的情况下)

然后,文件x1的内容是:

Script started on Thu Jun 13 14:51:29 2013
$ echo "Hi this is a dude"
Hi this is a dude
$ echo "done"
done
$ exit
exit

Script done on Thu Jun 13 14:51:52 2013

使用基本的shell脚本(
grep-v
,特别是当Unix提示符具有独特的子字符串模式时),您可以轻松编辑自己的命令和开始/结束行。

可以在bash中使用
实现输出重定向:

您可以运行任何带有端口输出的程序,其所有输出都将进入一个文件,例如:

$ ls > out
$ cat out
Desktop
Documents
Downloads
eclipse
Firefox_wallpaper.png
...
$ bash > outfile
$ echo "hello"
$ echo "this is an outfile"
$ cd asdsd
bash: cd: asdsd: No such file or directory
$ exit
exit
$ cat outfile
hello
this is an outfile
$

$ bash &> outfile
echo "hi"
echo "this saves everythingggg"
cd asdfasdfasdf
exit
$ cat outfile
hi
this saves everythingggg
bash: line 3: cd: asdfasdfasdf: No such file or directory
$
因此,如果您想使用移植的输出打开一个新的shell会话,只需这样做!:

$ bash > outfile
将启动一个新的bash会话,将所有标准输出移植到该文件

$ bash &> outfile
将所有stdout和stderr移植到该文件(意味着您将不再看到终端中显示的提示)

例如:

$ ls > out
$ cat out
Desktop
Documents
Downloads
eclipse
Firefox_wallpaper.png
...
$ bash > outfile
$ echo "hello"
$ echo "this is an outfile"
$ cd asdsd
bash: cd: asdsd: No such file or directory
$ exit
exit
$ cat outfile
hello
this is an outfile
$

$ bash &> outfile
echo "hi"
echo "this saves everythingggg"
cd asdfasdfasdf
exit
$ cat outfile
hi
this saves everythingggg
bash: line 3: cd: asdfasdfasdf: No such file or directory
$

可以在bash中使用
实现输出重定向:

您可以运行任何带有端口输出的程序,其所有输出都将进入一个文件,例如:

$ ls > out
$ cat out
Desktop
Documents
Downloads
eclipse
Firefox_wallpaper.png
...
$ bash > outfile
$ echo "hello"
$ echo "this is an outfile"
$ cd asdsd
bash: cd: asdsd: No such file or directory
$ exit
exit
$ cat outfile
hello
this is an outfile
$

$ bash &> outfile
echo "hi"
echo "this saves everythingggg"
cd asdfasdfasdf
exit
$ cat outfile
hi
this saves everythingggg
bash: line 3: cd: asdfasdfasdf: No such file or directory
$
因此,如果您想使用移植的输出打开一个新的shell会话,只需这样做!:

$ bash > outfile
将启动一个新的bash会话,将所有标准输出移植到该文件

$ bash &> outfile
将所有stdout和stderr移植到该文件(意味着您将不再看到终端中显示的提示)

例如:

$ ls > out
$ cat out
Desktop
Documents
Downloads
eclipse
Firefox_wallpaper.png
...
$ bash > outfile
$ echo "hello"
$ echo "this is an outfile"
$ cd asdsd
bash: cd: asdsd: No such file or directory
$ exit
exit
$ cat outfile
hello
this is an outfile
$

$ bash &> outfile
echo "hi"
echo "this saves everythingggg"
cd asdfasdfasdf
exit
$ cat outfile
hi
this saves everythingggg
bash: line 3: cd: asdfasdfasdf: No such file or directory
$

从shell启动的命令继承用于shell标准输出的文件描述符。在典型的交互式shell中,标准输出是终端。您可以通过使用
exec
命令来更改:

exec > output.txt
在该命令之后,shell本身将把它的标准输出写入一个名为output.txt的文件,并且它生成的任何命令都会这样做,除非另有重定向。您始终可以使用“恢复”输出到终端

exec > /dev/tty

请注意,您的shell提示符和在提示符下键入的文本将继续显示在屏幕上(因为shell将这两个命令都写入标准错误,而不是标准输出)。

从shell启动的命令将继承用于shell标准输出的文件描述符。在典型的交互式shell中,标准输出是终端。您可以通过使用
exec
命令来更改:

exec > output.txt
在该命令之后,shell本身将把它的标准输出写入一个名为output.txt的文件,并且它生成的任何命令都会这样做,除非另有重定向。您始终可以使用“恢复”输出到终端

exec > /dev/tty

请注意,您的shell提示符和在提示符下键入的文本将继续显示在屏幕上(因为shell会将这两个命令都写入标准错误,而不是标准输出)。

如果您希望查看输出并将其写入文件(例如用于以后的分析),则可以使用
tee
命令

$ echo "hi this is a dude" | tee hello
hi this is a dude
$ ls
hello
$ cat hello
hi this is a dude

tee是一个有用的命令,因为它允许您存储进入其中的所有内容并在屏幕上显示。特别适用于记录脚本的输出。

如果您希望查看输出并将其写入文件(例如用于以后的分析),则可以使用
tee
命令

$ echo "hi this is a dude" | tee hello
hi this is a dude
$ ls
hello
$ cat hello
hi this is a dude

tee是一个有用的命令,因为它允许您存储进入其中的所有内容并在屏幕上显示。特别适用于记录脚本输出。

查看此问答:查看此问答:酷。改进:
exec>>(tee-a output.txt)
让输出进入文件和terminal.Cool。一个改进:
exec>>(tee-a output.txt)
让输出进入文件和终端。