2017-06-14 74 views
-1

我是bash腳本編程的初學者。當我測試自己時,我遇到了這個問題。從用戶獲取一個整數作爲輸入,乘以5,並輸出結果是否大於14 我的劇本有所解決此在bash中的算術計算

#!/bin/bash 
echo "Insert an Integer" 
read input 
echo $((input*5)) 
num1 =$((input*5)) 
num2 =14 
if [ $num1 \> $b ]; 
then 
    echo "a is greater than b"; 
else 
    echo "b is greater than a"; 
fi; 

我能得到一些幫助?

回答

0

(())將被用於所有的算術運算,包括測試支架,所以你可以這樣做:

#!/bin/bash 

read -p "Enter an integer: " input 

echo "You entered: $input" 

((num1 = input * 5)) 

echo "$input multiplied by 5 is $num1" 

num2=14 

if ((num1 > num2)) 
then 
    echo "$input multiplied by 5 is greater than 14" 
else 
    echo "$input multiplied by 5 is NOT greater than 14" 
fi 
0

我在這裏看到一些問題。 $b突然從哪裏來? 什麼是a和b?爲什麼回聲陳述談論a & b

您還有一些明顯的語法錯誤,如果您運行該腳本,您會看到這些錯誤。

試試這個:

#!/bin/bash 
echo "Enter an Integer" 
read input 
echo "You entered: $input" 
num1=$((input*5)) 
echo "$input multiplied by 5 is $num1" 
num2=14 
if [ $num1 -gt $num2 ]; 
then 
     echo "$input multiplied by 5 is greater than 14" 
    else 
     echo "$input multiplied by 5 is NOT greater than 14" 
fi