我正在学习exec命令。我知道该exec命令将替换启动它的进程。因此,不可能返回到被exec命令替换的过程。
可以说,这样做的目的是使不可能返回到启动它的过程。
但是,像exec命令这样可用的东西还有什么其他用途呢?
exec ssh server-that-does-byobu-on-login.example.com很高兴,因为^ A ^ D完全关闭了终端。(更一般地,exec byobu(或exec screen,exec tmux)。
我正在学习exec命令。我知道该exec命令将替换启动它的进程。因此,不可能返回到被exec命令替换的过程。
可以说,这样做的目的是使不可能返回到启动它的过程。
但是,像exec命令这样可用的东西还有什么其他用途呢?
exec ssh server-that-does-byobu-on-login.example.com很高兴,因为^ A ^ D完全关闭了终端。(更一般地,exec byobu(或exec screen,exec tmux)。
Answers:
exec 主要用于包装脚本中。
如果要在执行主程序之前修改程序的环境,通常会编写一个脚本,并在脚本结束时启动主程序。但是,此时无需将该脚本保留在内存中。因此,exec在这些情况下使用以便主程序可以替换母脚本。
这是一个实际的例子。它的mate-terminal.wrapper脚本带有mate-terminal。mate-terminal通过检查用户的环境,它以一些额外的参数开始。
#!/usr/bin/perl -w
my $login=0;
while ($opt = shift(@ARGV))
{
if ($opt eq '-display')
{
$ENV{'DISPLAY'} = shift(@ARGV);
}
elsif ($opt eq '-name')
{
$arg = shift(@ARGV);
push(@args, "--window-with-profile=$arg");
}
elsif ($opt eq '-n')
{
# Accept but ignore
print STDERR "$0: to set an icon, please use -name <profile> and set a profile icon\n"
}
elsif ($opt eq '-T' || $opt eq '-title')
{
push(@args, '-t', shift(@ARGV));
}
elsif ($opt eq '-ls')
{
$login = 1;
}
elsif ($opt eq '+ls')
{
$login = 0;
}
elsif ($opt eq '-geometry')
{
$arg = shift(@ARGV);
push(@args, "--geometry=$arg");
}
elsif ($opt eq '-fn')
{
$arg = shift(@ARGV);
push(@args, "--font=$arg");
}
elsif ($opt eq '-fg')
{
$arg = shift(@ARGV);
push(@args, "--foreground=$arg");
}
elsif ($opt eq '-bg')
{
$arg = shift(@ARGV);
push(@args, "--background=$arg");
}
elsif ($opt eq '-tn')
{
$arg = shift(@ARGV);
push(@args, "--termname=$arg");
}
elsif ($opt eq '-e')
{
$arg = shift(@ARGV);
if (@ARGV)
{
push(@args, '-x', $arg, @ARGV);
last;
}
else
{
push(@args, '-e', $arg);
}
last;
}
elsif ($opt eq '-h' || $opt eq '--help')
{
push(@args, '--help');
}
}
if ($login == 1)
{
@args = ('--login', @args);
}
exec('mate-terminal',@args);
这里要注意的一点是,有一个exec调用,它将替换内存中的此脚本。
这是在unix和Linux StackExchange网站回答了类似的问题- https://unix.stackexchange.com/q/270929/19288
的另一个常见用法exec是重定向文件描述符。stdin,stdout,stderr可以被重定向到执行exec文件。
重定向stdout- exec 1>file将导致标准输出是一个以file当前Shell会话的结尾命名的文件。输出到显示器的所有内容都将在文件中。
重定向stdin-也可以用于将重定向stdin到文件。例如,如果要执行脚本文件script.sh,则可以stdin使用重定向到该文件exec 0<script.sh。
exec它是在文件描述符的重定向中使用的。
该exec 命令用指定的 命令替换当前的shell进程。通常,当您运行命令时,会生成(分叉)新进程。该exec 命令不会产生新进程。而是,当前过程被新命令覆盖。换句话说,exec在不创建新进程的情况下,该命令将代替当前外壳程序执行。
exec命令有三种最常见的用法:
示例1:
如果您bash以
$ bash
在pstree它看起来像
├─gnome终端
├─bash───bash───pstree
以前的bash外壳仍然在那里,您得到了一个新的bash外壳。而如果您打开一个新的bash shell,
$ exec bash
该pstree节目
├─gnome终端
├─bash───pstree
在这里旧bash的被新的代替。在一个命令中退出多次登录特别有用。它更安全,并且消除了错误地离开开放终端的机会。请参阅使用一个命令从root和user退出
示例2:您可以将文件打开为
$ exec vi filename.txt
退出时vi,由于外壳已被更换,因此无需单独关闭终端。一旦关闭vi,终端也将关闭。
该exec 命令还可用于动态打开,关闭和复制文件描述符的shell脚本中。这样就可以将STDIN,STDERR,STDOUT和其他文件描述符重定向到Shell脚本中的各种文件,而不是命令调用字符串。如果未指定命令或参数,则可以指定重定向符号和文件描述符以执行这些功能。
假设您有一个Shell脚本script.sh想要一个日志文件script.log,您可以将其exec用作,
LOG=/path/to/script.log
exec 1>>$LOG
exec 2>&1
相当于
./script &>> /path/to/script.log
./script >> /path/to/script.log 2>&1
您还可以使用exec命令来创建一组shell脚本,这些脚本相互依次执行,就像过程的各个阶段一样。您不必每次都需要将控件转移到下一个脚本时才生成新进程,而是执行exec命令。
在这种情况下,每个阶段的最后一条语句应该是exec调用下一阶段的命令。
有关更多信息,请参见Shell脚本中命令的用法exec。
注意:以上部分摘自此。