2つの数字を入力し、「a」を入力すると加算し、「s」を入力すると減算します。

2つの数字を入力し、「a」を入力すると加算し、「s」を入力すると減算します。

だからこのコードに問題があります。実行しようとすると、メッセージ行12:0:コマンドが見つかりません。


#!/bin/bash

let results=0;

echo "First number please"

read num1

echo "Second mumber"

read num2

echo "Enter operation a=add, s=subtract, m=multiply, and d=divide"

read Op

if [ "$Op" = "a" ]

then

results=$((num1+num2))

elif [ "$Op" = "s" ]

then

results=$((num1-num2))

elif [ "$Op" = "d" ]

then

results=$((num1/num2))

elif [ "$Op" = "m" ]

then

results=$((num1*num2))

fi

ベストアンサー1

elif は最後の else と同様に有効なキーワードではありません。使用:

If *condition 1*
    #Do Something
elif *condition 2*
    #Do Something Else
else
    #Do this as a last resort
fi

Else If は、bc の答えのように文字列に変換しない場合は else が必要です。

引用:4 Bash Ifステートメントの例

おすすめ記事