如何重做shell脚本中的最后一个命令?
问题描述:
#!/bin/bash
function main_menu()
{
echo Simple Linux Utility
echo 0) Repeat last command
read -p "Choose your option : " input
case $input in
0)
!!;;
*)
exit;;
esac
}
main_menu
exit 0
的问题是: “简单重复的最后命令中使用的变量要记住的最后一个数字。” 和我无法写入任何数据到外部文件如何重做shell脚本中的最后一个命令?
但我不吨知道如何重复最后的命令在shell脚本 我不能使用! !-1在shell脚本中,它会显示为错误
答
我会提出这样一个小解决方案。 在shell函数中我添加了一个循环,所以你可以选择最后执行的命令。
function main_menu()
{
echo Simple Linux Utility
echo 0: Repeat last command
while [ true ]
do
read -p "Choose your option : " input
case $input in
"0") ;;
"1") cmd="ls -las" ;;
"2") cmd="uname -a" ;;
*) exit ;;
esac
$cmd
done
}
main_menu
exit 0
+0
您可能想要添加选项'read -p'自定义命令:“cmd”。我想你会喜欢内置的'select'。 –
+0
我推荐'而真实;做'或'时:'; do'。 'while [true];做'的工作,但'同时[假]; do',如果你认为'[true]'在某种程度上评估一个布尔值,这是违反直觉的。当底层语义完全不同时,不要模仿其他语言。 – chepner
我没有看到脚本... – rbaleksandar
我认为[这](http://unix.stackexchange.com/questions/147563/how-do-i-repeat-the-last-command-without-使用箭头键)可以帮助你。 – ganchito55