下面的shell脚本中有什么错误:
#!/bin/bash
total=0
for i in 1 2 3 4
do
echo -n "Enter Passenger Name: "
read pname
echo -n "Enter Car Number: "
read ncar
echo -n "Enter Amount: "
read amount
echo -n "Enter Distance Travelled: "
read dtravelled
total=$((total + dtravelled))
average=$(echo "$total 4" | awk '{print $1/$2}')
if test $dtravelled -gt 10000 -a $dtravelled -le 7000
then
bpassenger=$((business_pass + 1))
elif test $dtravelled -gt 7000 -a $dtravelled -le 5000
then
lpassenger=$((leisure_pass + 1))
else
opassenger=$((other_pass + 1))
fi
if test $i -eq 1
then
lname=$pname
low=$dtravelled
fi
if test $dtravelled -lt $low
then
lname=$pname
low=$dtravelled
fi
echo
done
echo "Total of distance travelled by the passengers: $total"
echo "Average distance travelled by the passengers: $average"
echo "Number of business passengers: $bpassenger"
echo "Number of leisure passengers: $lpassenger"
echo "Number of other passengers: $opassenger"
echo "lowest distance travelled is $low by $lname."
***未计算下面的块***在此计算中总是得到空输出
if test $dtravelled -gt 10000 -a $dtravelled -le 7000
then
bpassenger=$((business_pass + 1))
elif test $dtravelled -gt 7000 -a $dtravelled -le 5000
then
lpassenger=$((leisure_pass + 1))
else
opassenger=$((other_pass + 1))
fi
输出:
bash mySript.sh
Enter Passenger Name: ahmad
Enter Car Number: 9889
Enter Amount: 20
Enter Distance Travelled: 1100
Enter Passenger Name: sara
Enter Car Number: 5214
Enter Amount: 300
Enter Distance Travelled: 1120
Enter Passenger Name: mohammad
Enter Car Number: 9713
Enter Amount: 6
Enter Distance Travelled: 500
Enter Passenger Name: nora
Enter Car Number: 98765
Enter Amount: 41
Enter Distance Travelled: 7500
Total of distance travelled by the passengers: 10220
Average distance travelled by the passengers: 2555
***Number of business passengers:***
***Number of leisure passengers:***
Number of other passengers: 1
lowest distance travelled is 500 by mohammad.
2条答案
按热度按时间7hiiyaii1#
当用普通的数学符号写
test $dtravelled -gt 10000 -a $dtravelled -le 7000
时,你会发现它永远不可能为真:行驶里程〉10000 * 与 * 行程〈7000
10000〈行程〈7000
⇒ 10000〈7000
⇒错误
7000和5000的其他测试相同。
调整你的逻辑。如果你再次遇到问题,在脚本的开头添加
set -x
或者用bash -x scriptFile.sh
执行它。这将打印出执行的语句和变量内容,这将使调试更容易。pjngdqdw2#
我相信您可以在这行代码中找到所有内容:
...应该变成:
当你给一个变量赋值时,你只需把变量的名字放在等号的左边:
当你使用一个变量的值时,你在等号的右边加上一个美元符号: