gpt4 book ai didi

bash - bash 中的 if else 语句

转载 作者:行者123 更新时间:2023-12-04 18:54:59 24 4
gpt4 key购买 nike

我想让用户输入选择,然后使用 if elif 语句来区分输入。

代码:

read in

if $in = "1"
then
Type="Employee1"
elif $in = "2"
then
Type="Employee2"
elif $in = "3"
then
Type="Employee3"
else
echo "Wrong input"

错误:
./Pay.sh: line 127: 1: command not found
./Pay.sh: line 130: 1: command not found
./Pay.sh: line 133: 1: command not found
Wrong input

请指教谢谢

最佳答案

方括号是必需的。

if [[ $in = "1" ]]; then
Type="Employee1"
elif [[ $in = "2" ]]; then
Type="Employee2"
elif [[ $in = "3" ]]; then
Type="Employee3"
else
echo "Wrong input"
fi

对于 if 的特定链您也可以使用 case陈述。
case $in in
1) Type='Employee1';;
2) Type='Employee2';;
3) Type='Employee3';;
*) echo 'Wrong input' >&2;;
esac

关于bash - bash 中的 if else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19730037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com