gpt4 book ai didi

linux - 为什么这个 c-shell 脚本会停止?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:04:02 24 4
gpt4 key购买 nike

该脚本应该打印“输入要添加的巢号:”并继续这样做,直到用户输入一个负数。此时应该打印正数的总和。然而,由于循环请求下一个数字一次,它被输入,然后不再询问,脚本只是停止做任何事情,甚至没有到达循环中的下一行。

#!/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

最佳答案

$<从 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

关于linux - 为什么这个 c-shell 脚本会停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16048286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com