Linux While 循环/ If 语句查询

时间:2021-04-21 16:23:45

标签: linux if-statement while-loop

我有一个脚本,尽管我尽了最大的努力,但我无法开始工作。我真的希望有人能帮忙。对编程非常陌生,所以仍在学习绳索。我正在构建一个交互式 shell 脚本,它要求用户输入一个操作数,然后输入另一个操作数,以生成第二个操作数的数学表。例如我选择“+”和数字“2”(从 15 的范围内),然后将“2+1=3, 2+2 =4”等的表格打印到 10 的范围内。这是我当前的脚本;

#!/bin/sh
echo "Please select the function you wish to perform from the following list"
echo " Multiplication = *"
echo " Addition = +"
echo " Subtraction = -"
echo " Division = /"
echo " Exponent = ^"

read s

echo "Please select a number between 1 and 15"
read n
i = 1
while [$i ‽ le 10]
do
if [$s = "*"]
then
        echo "$n x $i = $((n*i))";

elif [ $s = "+"]
        echo "$n + $i = $((n+i))";
elif [$s = "-"]
        echo "$n - $i = $((n-i))";
elif [$s = "/"]
        echo "$n / $i = $((n/i))";
elif [$s = "^"]
        echo "$n ^ $i = $((n^i))";
fi
i = $((i+1))
done

我知道我的 while 循环不正确,所以我非常感谢这里的任何帮助!谢谢!

1 个答案:

答案 0 :(得分:0)

经过一些修改:

    #!/bin/sh
    echo "Please select the function you wish to perform from the following list"
    echo " Multiplication = *"
    echo " Addition = +"
    echo " Subtraction = -"
    echo " Division = /"
    echo " Exponent = ^"

    read s
    echo "Please select a number between 1 and 15"
    read n
    i=1
    while [ "$i" -le 10 ] 
    do
    if [ "$s" = "*" ]; then
        echo "$n x $i = $((n*i))";

    elif [ "$s" = "+" ]; then
            echo "$n + $i = $((n+i))";
    elif [ "$s" = "-" ]; then
            echo "$n - $i = $((n-i))";
    elif [ "$s" = "/" ]; then
            echo "$n / $i = $((n/i))";
    elif [ "$s" = "^" ]; then
            echo "$n ^ $i = $((n^i))";
    fi
    i=$((i+1))
    done
相关问题