我应该在哪里修改给定的 shell 脚本?

我应该在哪里修改给定的 shell 脚本?

这是我的输入:

#!/bin/bash
#Example of menu programs
clear
echo "*******************"
echo "1.Date"
echo "2.List of users"
echo "3.Open a file"
echo "4.delete a file"
echo "5.Exit"

echo "Enter a choice[1-5] :"
read choice
case $choice in
1) echo "Today date is : `date`";;
2) who ;;
3) `touch file`;;
4) `rm -rf kk`;;
5) exit ;;
*) echo "choice wrong. try again";;
esac

这是我的输出:

*******************
1.Date
2.List of users
3.Open a file
4.delete a file
5.Exit
Enter a choice[1-5] :
1
Today date is : Sun Jul 19 08:07:41 PDT 2015

但我并不期待这个输出。

我期待什么:

*******************
1.Date
2.List of users
3.Open a file
4.delete a file
5.Exit
Enter a choice[1-5] :
1
Today date is : Sun Jul 19 08:07:41 PDT 2015

*******************

1.Date
2.List of users
3.Open a file
4.delete a file
5.Exit
Enter a choice[1-5] :

我怎样才能实现这个目标?

答案1

将其放入循环中:

while true; do
    echo "*******************"
    echo "1.Date"
    echo "2.List of users"
    echo "3.Open a file"
    echo "4.delete a file"
    echo "5.Exit"

    echo "Enter a choice[1-5] :"
    read choice
    case $choice in
    1) echo "Today date is : `date`";;
    2) who ;;
    3) `touch file`;;
    4) `rm -rf kk`;;
    5) break ;;
    *) echo "choice wrong. try again";;
    esac
done

或者将您的脚本放入循环中

while true; do
     menu.sh
done

答案2

#!/bin/bash
#Example of menu programs
while true;
do
    echo "*******************"
    echo "1.Date"
    echo "2.List of users"
    echo "3.Open a file"
    echo "4.delete a file"
    echo "5.Exit"
    echo "*******************"

    echo "Enter a choice[1-5] :"
    read choice
    case $choice in
        1) echo "Today date is : `date`";;
        2) who ;;
        3) `touch file`;;
        4) `rm -rf kk`;;
        5) break ;;
        *) echo "Wrong choice. Please try again";;
    esac
done

供你参考:Unix - Shell 循环控制

相关内容