带菜单的 Bash Shell 脚本

rjd*_*ght 3 linux bash

我编写了一个 bash shell 脚本(下面提供了代码),它为用户提供了 4 个选项。但是,我在代码方面遇到了一些麻烦。现在,当他们选择选项 3 以显示日期时。它一遍又一遍地循环。我必须关闭终端窗口才能停止它,因为它是一个无限循环。我将如何防止这种情况?退出似乎也不起作用。

如果有人能帮我一下,谢谢。

#!/bin/bashe
 echo -n "Name please? "
 read name
 echo "Menu for $name
    1. Display a long listing of the current directory
    2. Display who is logged on the system
    3. Display the current date and time
    4. Quit "
read input
input1="1"
input2="2"
input3=$(date)
input4=$(exit)
while [ "$input" = "3" ]
do
echo "Current date and time: $input3"
done

while [ "$input" = "4" ]
do
echo "Goodbye $input4"
done
Run Code Online (Sandbox Code Playgroud)

gle*_*man 9

一个紧凑的版本:

options=(
    "Display a long listing of the current directory"
    "Display who is logged on the system"
    "Display the current date and time"
    "Quit" 
)

PS3="Enter a number (1-${#options[@]}): "

select option in "${options[@]}"; do
    case "$REPLY" in 
        1) ls -l ;;
        2) who ;;
        3) date ;;
        4) break ;;
    esac
done
Run Code Online (Sandbox Code Playgroud)