如何重做shell脚本中的最后一个命令?

时间:2016-03-05 13:33:28

标签: linux shell redhat

#!/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 个答案:

答案 0 :(得分:1)

我会提出这样一个小解决方案。 在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