调用'脚本'命令并从一个bash脚本中退出它

我正在使用linux脚本命令http://www.linuxcommand.org/man_pages/script1.html在交互式bash脚本中logging所有input和输出。

此刻我必须调用script命令,然后运行我的bash脚本,然后exit

我想运行script并从实际的bash脚本本身中exit命令。 我怎样才能做到这一点?

我已经尝试过script -a但是这对交互式脚本不起作用。

任何援助将不胜感激。

“脚本”分叉shell(或用“-c”指定的命令),所以我不认为你可以从你想要脚本的脚本中调用“脚本”。

以这个(恼人的)交互式shell脚本(称为“fred”):

 #!/bin/bash while read -p 'Pete and Repeat were on a boat. Pete jumped off. Who was left? ' WHO do : done 

运行它并将交互“脚本化”到一个文件中:

 [myles@marklar ~]$ script -c ~/fred fred.log Script started, file is fred.log Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat Pete and Repeat were on a boat. Pete jumped off. Who was left? Script done, file is fred.log 

日志文件包含:

 [myles@marklar ~]$ cat fred.log Script started on Mon 12 Nov 2012 11:44:41 PM EST Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat Pete and Repeat were on a boat. Pete jumped off. Who was left? Script done on Mon 12 Nov 2012 11:44:51 PM EST 

总之,就我所知,你必须用“脚本”命令来调用你的交互式shell脚本。

您可以使用以下结构将脚本的所有输出redirect到文件:

 exec &> /your/script.log 

另外,bash允许你logging它执行的所有命令,它接收到的所有input,以及当你添加以下内容时执行的所有扩展:

 set -x 

如果这些是脚本的前两行,那么脚本所做的所有事情都将被logging下来。