记录终端的命令不能与bash一起使用

时间:2015-12-19 06:11:03

标签: linux bash ubuntu

我想使用“script”命令,我有以下代码

#!/bin/bash
script & 
wait
echo "hello"
echo "hello2"
pid=$(pidof script | awk '{print $1}')
kill -9 $pid

我需要脚本命令来捕获输出,但在命令“script&”之后输出是:

         Script started, file is typescript             
         Script done, file is typescript

并且脚本不会记录任何内容,不知道为什么?

1 个答案:

答案 0 :(得分:2)

有两种方法可以使用script命令:

  • 仅保存代码的输出(即批处理模式)

    $ script filename bash -c 'echo foo; echo bar'
    

    将输出

    Script started, file is filename
    foo
    bar
    Script done, file is filename
    
  • 保存终端上显示的所有内容(即交互模式)。要结束脚本,只需键入exit或按Ctrl-D

    即可
    $ script filename
    Script started, file is filename
    $ echo foo
    foo
    $ echo bar
    bar
    $ exit
    exit
    Script done, file is filename
    

请注意,批处理方式是使用script的交互式经典方式的黑客攻击。

在您的情况下,只需忘记&kill内容,并在希望脚本结束时按Ctrl-D。