有时我必须连续多次运行命令,例如查看服务是否已启动,将手移离正常输入位置以反复按向上箭头和输入键变得乏味.有没有办法在没有Up Arrow和Enter键的情况下运行上一个命令,可能还有一个精心设计的shell脚本?
我已经尝试了以下内容,但它不能令人满意,因为它无法执行别名,而且速度有点慢.
history | tail -2 | head -1 | cut -d' ' -f4- | cat > prev_command.txt
sleep .01
chmod 777 prev_command.txt
eval prev_command.txt
rm prev_command.txt
理想情况下,我有一个这个脚本的别名,所以我可以在命令行中键入类似“prev”的内容,然后按Enter键再次运行上一个命令.
最佳答案
在bash中:
$help fc fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command] Display or execute commands from the history list. fc is used to list or edit and re-execute commands from the history list. FIRST and LAST can be numbers specifying the range,or FIRST can be a string,which means the most recent command beginning with that string. Options: -e ENAME select which editor to use. Default is FCEDIT,then EDITOR,then vi -l list lines instead of editing -n omit line numbers when listing -r reverse the order of the lines (newest listed first) With the `fc -s [pat=rep ...] [command]' format,COMMAND is re-executed after the substitution OLD=NEW is performed. A useful alias to use with this is r='fc -s',so that typing `r cc' runs the last command beginning with `cc' and typing `r' re-executes the last command. Exit Status: Returns success or status of executed command; non-zero if an error occurs.
注意别名r的建议;我经常使用它.