脚本应该打印“输入要添加的嵌套号:”并继续这样做,直到用户输入负数。此时应该打印正数的总和。然而,循环请求下一个数字,输入,然后不再询问,脚本只是停止做任何事情,甚至没有到达循环中的下一行。
#!/bin/csh -x
#
# This script adds positive numbers entered by the user, stopping
# when a negative number is added
# Usage: +#, +#, +#... -#.
#
@ x=0
@ sum = 0
while($x>= 0)
echo -n "Enter the next number to be added: "
@ sum = $sum + $<
@ x = $<
end
#
exit 0
答案 0 :(得分:1)
$<
从stdin读取一行。必须将此值分配给变量,否则如果第二次使用$<
,脚本将在继续之前进一步输入。
@ x=0
@ sum = 0
while ($x >= 0)
echo -n "Enter the next number to be added: "
@ x = $<
if ($x >= 0) then
@ sum = $sum + $x
endif
end
echo $sum
exit 0