问题描述
我正在尝试从脚本中获取命令行中最后执行的命令,以供以后参考:
I'm trying to get last executed command from command line from a script to be saved for a later reference:
示例:
# echo "Hello World!!!"
> Hello World!!!
# my_script.sh
> echo "Hello World!!!"
,脚本的内容为:
#!/usr/bin/ksh
fc -nl -1 | sed -n 1p
现在您在这里使用ksh
和fc
注意到,它是一个内置命令,如果正确理解,则应由任何POSIX兼容shell来实现. [我知道此功能是交互式的,再次调用相同的fc
会产生不同的结果,但这不是要讨论的问题.
Now as you notices using here ksh
and fc
is a built in command which if understood correctly should be implemented by any POSIX compatible shells. [I understand that this feature is interactive and that calling same fc
again will give different result but this is not the concern do discuss about]
仅当从同样是ksh
的shell中调用my_script.sh
时,或者如果从bash
调用并将脚本的第一行更改为#!/bin/bash
时,上述命令才能正常工作,并且外壳是否不同也没有.
Above works so far so good only if my_script.sh
is being called from the shell which is as well ksh
, or if calling from bash
and changing 1st line of script as #!/bin/bash
then it works too and it doesn't if shells are different.
我想知道是否有什么好的方法可以实现上述目的而不受调用脚本的shell的限制.我知道fc
是内置命令,它在每个shell中都有效,因此很可能我的方法在我想要实现的目标上根本不好.还有更好的建议吗?
I would like to know if there is any good way to achieve above without being constrained by the shell your script is called from. I understand that fc
is a built in command and it works per shell thus most probably my approach is not good at all from what I want to achieve. Any better suggestions?
推荐答案
我实际上尝试过此操作,但是不能在不同的Shell之间一致地完成此操作.
I actually attempted this, but it cannot be done between different shells consistently.
而 fc
-l`,是用于显示$ SHELL历史记录的POSIX标准命令,实现细节可能有所不同.
Whilefc
-l`,is the POSIX standard command for showing $SHELL history, implementation details may be different.
至少 bash 和 ksh93 都将报告上一条命令,
At least bash and ksh93 both will report the last command with
fc -n -l -1 -1
但是,POSIX不保证将外壳程序历史记录继承到外壳程序的新实例,因为这需要存在$HISTFILE
.如果没有目前,shell可能默认为$HOME/.sh_history
.
However, POSIX does not guarantee that shell history will be carried over to a new instance of the shell, as this requires the presence of a $HISTFILE
. If none ispresent, the shell may default to $HOME/.sh_history
.
但是,此历史记录文件或命令历史记录列表不能在不同的shell之间移植.
However, this history file or Command History List is not portable between different shells.
POSIX Shell的描述命令历史记录列表说:
The POSIX Shell description of theCommand History List says:
重点增强
这意味着您的脚本需要知道哪个shell编写了该历史记录.
What this means is that for your script needs to know which shell wrote that history.
我尝试使用$SHELL -c 'fc -nl -1 -1'
,但是当$ SHELL指向bash时,这似乎不起作用.从bash调用ksh -c
...确实有效.
I tried to use $SHELL -c 'fc -nl -1 -1'
, but this did not appear to work when $SHELL refers to bash. Calling ksh -c
... from bash actually worked.
我要使其正常工作的唯一方法是创建一个函数.
The only way I could get this to work is by creating a function.
last_command() { (fc -n -l -1 -1); }
在ksh 和 bash中,这将产生预期的结果.此函数的变体可用于将结果写入其他位置.但是,只要调用它,它就会中断来自与当前流程不同的流程.
In both ksh and bash, this will give the expected result. Variations of this function can be used to write the result elsewhere. However, it will break whenever it's calledfrom a different process than the current.
您能做的最好的事情就是创建这些功能并将其来源到您的交互式外壳.
The best you can do is to create these functions and source them into yourinteractive shell.
这篇关于从脚本获取最后执行的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!