Shell脚本在单个文件中接受多个用户输入

nin*_*top 1 unix linux shell

shell脚本应该在单行输入中采用多个条件,并且它应该采用输入字符的一端来执行下一个操作.即.

#!/bin/bash
#Functions are defined here 1 2 3 4 5


echo "choice"
echo 
echo "[1] one"
echo "[2] two"
echo "[3] three"
echo "[4] four"
echo "[5] five"
echo
read -p "Enter choice:  " ch
        if [ "$ch" = "1" ]; then
        function_1
        else
        if [ "$ch" = "2" ]; then
        function_2
        else
        if [ "$ch" = "3" ]; then
        function_3
        else
        if [ "$ch" = "4" ]; then
        function_4
        else
        if [ "$ch" = "5" ]; then
        function_5
fi
fi
fi
fi
fi
Run Code Online (Sandbox Code Playgroud)

现在说输入结束用"e"表示,因此如果我执行.sh文件并在"输入选择"中

$Enter choice: 1 3 5 e
Run Code Online (Sandbox Code Playgroud)

它应该逐个执行1 3和5功能怎么做?

Max*_*ulo 5

您可以迭代所有选项,直到找到"输入结束":

read -p "Enter choice:  " ch

for choice in $ch; do
    [ "$choice" == 'e' ] && break
    eval function_$choice
done
Run Code Online (Sandbox Code Playgroud)

注意:eval将从参数汇编命令,然后通过shell执行它