我想用“脚本”命令,我有下面的代码
#!/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
脚本不logging什么,为什么?
这是你应该这样做的:
script <output-file> <commands>
例:
script typescript bash -c 'echo "hello"; echo "hello2"' Script started, output file is typescript hello hello2 Script done, output file is typescript
然后检查创建的输出文件:
cat typescript Script started on Sat Dec 19 01:54:04 2015 hello hello2 Script done on Sat Dec 19 01:54:04 2015
有两种方法可以使用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,当你想脚本结束。