我想编写一个perl脚本,它可以执行与fc类似的操作。实际上我想以编程方式编辑最后执行的命令。 这样的事情应该有效
perl -wle 'print system("/bin/bash", "-i", "-c", "history | tail -n 1")'
但是system
返回执行的命令的退出状态,而不是stdout,这就是我想要的,而我无法使用返回激活历史记录(-i
标志) -tic,qx//
或打开烟斗。
我知道使用back-tic,qx//
或打开一个管道,读取命令的stdout很简单,但在这种情况下如何正确使用builtin bash hisotry
命令? / p>
即使使用system
并将-i
传递给bash,我也无法从history | tail -n 1
获得预期的输出。 Bi将输出重定向到文件,我发现其内容为空。
perl -wle 'print system("/bin/bash", "-i", "-c", "history | tail -n 1 > /tmp/pippo")'
我是否被迫在文件whit history -w
上编写bash历史记录并在perl中读取该文件?
答案 0 :(得分:0)
使用qx捕获shell命令输出。直接读取~/.bash_history
文件,因为您无法捕获shell内置函数history
的输出。
您可能需要将history -a
添加到.bashrc
文件中,以便bash在每个命令后写入历史记录文件,或者将history -w
添加到您的bash PROMPT_COMMAND
。其他设置在unix.stackexchange.com完全覆盖。
$ echo "foo"
foo
$ perl -e 'chomp(my $cmd = qx(tail -n 1 ~/.bash_history)); print "$cmd bar\n";'
echo "foo" bar