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

标签 linux shell unix

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

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

相关文章:

linux - 检查脚本中的差异退出状态

linux - 将数据作为字节发送到 bash 中的管道

python - 如何轻松制作自己的自定义setup.py命令?

python - shell multipipe 被多个 python 脚本破坏

php - 从终端验证启用 SSL 的网站

linux - request_irq 成功但从未检测到中断

linux - 列中的最小值、平均值和最大值

unix 命令行 ...如何grep 并仅显示包含字符串的文件名?

linux - 终止进程——从 allproc 列表到 zombieproc 列表的转换

c++ - 我可以在 Mac 上使用 C++ 获得 -1 退出代码吗?