我不明白 bash 执行
I don't understand bash exec
#!/bin/bash
#...
exec >> logfile
#cmd
我猜这是保存输出。
你能用同样的例子扩展它吗?
非常感谢!
我经常使用这个命令来列出 txt 文件:
find / -type f -exec grep -l "bash" {} \;
不过我也不是很懂
怎么会有这么好的人world.very很感动!
是的,它将任何进一步的输出发送到名为 logfile
的文件。换句话说,它将标准输出(也称为stdout)重定向到文件logfile
.
例子
让我们从这个脚本开始:
$ cat >script.sh
#!/bin/bash
echo First
exec >>logfile
echo Second
如果我们 运行 脚本,我们会看到第一个而不是第二个 echo
语句的输出:
$ bash script.sh
First
第二个 echo
语句的输出转到文件 logfile
:
$ cat logfile
Second
$
如果我们使用了 exec >logfile
,那么每次脚本 运行 时 logfile
都会被 覆盖 。因为我们使用 >>
而不是 >
,所以输出将 附加 到 logfile
。例如,如果我们再次 运行 它:
$ bash script.sh
First
$ cat logfile
Second
Second
文档
这记录在 man bash
:
exec [-cl] [-a name] [command [arguments]]
If command
is specified, it replaces the shell. No new process is created. The
arguments become the arguments to command. If the -l option is
supplied, the shell places a dash at the beginning of the zeroth
argument passed to command. This is what login(1) does. The -c
option causes command to be executed with an empty environment. If
-a is supplied, the shell passes name as the zeroth argument to the executed command. If command cannot be executed for some reason, a
non-interactive shell exits, unless the execfail shell option is
enabled. In that case, it returns failure. An interactive shell
returns failure if the file cannot be executed. If command is not
specified, any redirections take effect in the current shell, and the
return status is 0. If there is a redirection error, the return
status is 1. [Emphasis added.]
在您的例子中,没有指定 command 参数。因此,exec
命令执行重定向,在这种情况下,这意味着将任何进一步的标准输出发送到文件 logfile
.
查找命令并-exec
查找命令有一个 -exec
选项。例如:
find / -type f -exec grep -l "bash" {} \;
这里的-exec
除了名字相似外,与shell命令完全没有关系exec
.
构造 -exec grep -l "bash" {} \;
告诉 find
对它找到的任何文件执行 命令 grep -l "bash"
。这与 shell 命令 exec >>logfile
无关,该命令不执行任何操作,但具有重定向输出的效果。
您输出到标准输出的所有内容都不会输出到标准输出,而是输出到日志文件。
请参见下面的示例:
#!/bin/bash
# reassign-stdout.sh
LOGFILE=logfile.txt
exec 6>&1 # Link file descriptor #6 with stdout.
# Saves stdout.
exec > $LOGFILE # stdout replaced with file "logfile.txt".
# ----------------------------------------------------------- #
# All output from commands in this block sent to file $LOGFILE.
echo -n "Logfile: "
date
echo "-------------------------------------"
echo
echo "Output of \"ls -al\" command"
echo
ls -al
echo; echo
echo "Output of \"df\" command"
echo
df
# ----------------------------------------------------------- #
exec 1>&6 6>&- # Restore stdout and close file descriptor #6.
echo
echo "== stdout now restored to default == "
echo
ls -al
echo
exit 0
这会将您的脚本及其随后调用的任何其他程序生成的标准输出重定向到 logfile
。因此,如果您的脚本 运行 在执行后出现在终端中
exec >> logfile
您在终端 window 中看不到任何输出,但您会在 logfile
中找到它。 logfile
如果它不存在或每次您 运行 您的脚本时追加,将被创建。
#!/bin/bash
#...
exec >> logfile
#cmd
我猜这是保存输出。 你能用同样的例子扩展它吗? 非常感谢!
我经常使用这个命令来列出 txt 文件:
find / -type f -exec grep -l "bash" {} \;
不过我也不是很懂
怎么会有这么好的人world.very很感动!
是的,它将任何进一步的输出发送到名为 logfile
的文件。换句话说,它将标准输出(也称为stdout)重定向到文件logfile
.
例子
让我们从这个脚本开始:
$ cat >script.sh
#!/bin/bash
echo First
exec >>logfile
echo Second
如果我们 运行 脚本,我们会看到第一个而不是第二个 echo
语句的输出:
$ bash script.sh
First
第二个 echo
语句的输出转到文件 logfile
:
$ cat logfile
Second
$
如果我们使用了 exec >logfile
,那么每次脚本 运行 时 logfile
都会被 覆盖 。因为我们使用 >>
而不是 >
,所以输出将 附加 到 logfile
。例如,如果我们再次 运行 它:
$ bash script.sh
First
$ cat logfile
Second
Second
文档
这记录在 man bash
:
exec [-cl] [-a name] [command [arguments]]
If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does. The -c option causes command to be executed with an empty environment. If -a is supplied, the shell passes name as the zeroth argument to the executed command. If command cannot be executed for some reason, a non-interactive shell exits, unless the execfail shell option is enabled. In that case, it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1. [Emphasis added.]
在您的例子中,没有指定 command 参数。因此,exec
命令执行重定向,在这种情况下,这意味着将任何进一步的标准输出发送到文件 logfile
.
查找命令并-exec
查找命令有一个 -exec
选项。例如:
find / -type f -exec grep -l "bash" {} \;
这里的-exec
除了名字相似外,与shell命令完全没有关系exec
.
构造 -exec grep -l "bash" {} \;
告诉 find
对它找到的任何文件执行 命令 grep -l "bash"
。这与 shell 命令 exec >>logfile
无关,该命令不执行任何操作,但具有重定向输出的效果。
您输出到标准输出的所有内容都不会输出到标准输出,而是输出到日志文件。 请参见下面的示例:
#!/bin/bash
# reassign-stdout.sh
LOGFILE=logfile.txt
exec 6>&1 # Link file descriptor #6 with stdout.
# Saves stdout.
exec > $LOGFILE # stdout replaced with file "logfile.txt".
# ----------------------------------------------------------- #
# All output from commands in this block sent to file $LOGFILE.
echo -n "Logfile: "
date
echo "-------------------------------------"
echo
echo "Output of \"ls -al\" command"
echo
ls -al
echo; echo
echo "Output of \"df\" command"
echo
df
# ----------------------------------------------------------- #
exec 1>&6 6>&- # Restore stdout and close file descriptor #6.
echo
echo "== stdout now restored to default == "
echo
ls -al
echo
exit 0
这会将您的脚本及其随后调用的任何其他程序生成的标准输出重定向到 logfile
。因此,如果您的脚本 运行 在执行后出现在终端中
exec >> logfile
您在终端 window 中看不到任何输出,但您会在 logfile
中找到它。 logfile
如果它不存在或每次您 运行 您的脚本时追加,将被创建。