我想寫一個計算器腳本,但第一個if
阻止elif
。我的意思是,當我嘗試運行它時,我按1,但它運行,就好像我按了2.這是我的代碼。您的病情元素之間Bash如果阻止elif
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的內建'[['對於未加引號的變量更爲明智(它們被空字符串替代,所以它們不會「失去它們的位置」作爲參數)。 –