【问题标题】:Bash if blocking elif如果阻止 elif,则 Bash
【发布时间】:2017-06-29 14:33:35
【问题描述】:

我正在尝试编写一个计算器脚本,但第一个 if 阻止了 elif。我的意思是,当我尝试运行它时,我按 1,但它运行时就像按 2 一样。这是我的代码。

echo "For Advanced mode press 1"
echo "For Help press 2"

loop=1
while [ $loop=1 ]; do

    read n

    if [ $n=2 ]; then
            echo "To use the calculator follow the promptings."
            echo "If asked, the operators are: "
            echo "* for multiplication, / for division."
            echo "+ for addition, - for subtraction."
            echo "% for modulus, which is division remainder"
            echo "== is equal to, != is not equal to."
            echo "** for exponents" 
            n=""

    elif [ $n=1 ]; then
            read a
            break
    fi

done
echo "_______________________"
echo "What would you like to do?"
echo "Press 1 for basic arith"
echo -n "Press 2 for geometry"
read choice
loop=2

if [ $choice=1]; then
    while [ $loop=2 ]; do
        echo -n "Enter X Value: "
        read x
    echo -n "Enter Operator: "
    read op
    echo -n "Enter Y Value: "
    read y
    ans=$((x $op y))
    echo "$x $op $y = $ans"
    echo "____________________"
    echo "To input a new function, press enter"
    read cont
    done
fi

【问题讨论】:

    标签: bash if-statement scripting


    【解决方案1】:

    [ 内置函数用于评估条件。开头[之后必须有空格,结尾]之前必须有空格,否则语法错误。

    [ ... ] 表达式中,您可以使用各种条件运算符。一个这样的运算符是=。运算符前后必须加空格,否则不被识别为运算符。

    例如,[ $n=2 ],其中n 的值为1,将被评估为[ 1=2 ]1=2 不被评估为条件,因为它周围没有空格。 1=2 将被评估为字符串“1=2”。并且 Bash 中的任何非空字符串都是 truthy,因此语句 [ 1=2 ] 会产生 true,即使它不“看起来”它也会是 true。

    您必须编写[ "$n" = 2 ],以便= 被解释为条件运算符,并且表达式不会变成单个字符串。请注意,空格在 Bash 中非常重要。空格分隔程序的逻辑元素,没有空格的表达式连接为字符串。

    还请注意,我在变量周围添加了双引号。这是为了在变量为空的情况下保护表达式。例如,如果您编写 [ $n = 2 ] 并且变量为空,则表达式将被评估为 [ = 2 ],这是一个格式错误的表达式,脚本将因错误而崩溃。

    【讨论】:

    • 不错的答案。 [ 计算它给出的参数的数量,并根据该数量做不同的事情。另请注意,bash 的内置 [[ 对未加引号的变量更敏感(它们被空字符串替换,因此它们不会“失去位置”作为参数)。
    【解决方案2】:

    $n = 2 而不是$n=2 等条件元素之间放置空格。您应该将其参数作为空格分隔的单词传递。所以你的代码应该如下:

    echo "For Advanced mode press 1"
    echo "For Help press 2"
    
    loop=1
    while [ $loop = 1 ]; do
    
        read n
    
        if [ $n = 2 ]; then
            echo "To use the calculator follow the promptings."
            echo "If asked, the operators are: "
            echo "* for multiplication, / for division."
            echo "+ for addition, - for subtraction."
            echo "% for modulus, which is division remainder"
            echo "== is equal to, != is not equal to."
            echo "** for exponents" 
            n=""
    
        elif [ $n = 1 ]; then
            read a
            break
        fi
    
    done
    echo "_______________________"
    echo "What would you like to do?"
    echo "Press 1 for basic arith"
    echo -n "Press 2 for geometry"
    read choice
    loop=2
    
    if [ $choice = 1 ]; then
        while [ $loop = 2 ]; do
            echo -n "Enter X Value: "
            read x
        echo -n "Enter Operator: "
        read op
        echo -n "Enter Y Value: "
        read y
        ans=$((x $op y))
        echo "$x $op $y = $ans"
        echo "____________________"
        echo "To input a new function, press enter"
        read cont
        done
    fi
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2023-04-05
      • 2014-10-24
      • 2021-10-28
      • 1970-01-01
      相关资源
      最近更新 更多