C Shell脚本中While循环语法错误问题求助
C Shell脚本while循环语法错误及修复方案
错误分析
while循环条件语法错误
你的while循环条件$i <= num中,变量num未添加$符号。C Shell中引用变量必须以$开头,否则会被当作字符串字面量处理,导致表达式语法解析失败。正确写法应为while ($i <= $num)。平方计算方式错误
set Square =$i * $i`` 这行代码试图用反引号执行$i * $i,但这并非合法的系统命令,会引发执行错误。C Shell中进行算术运算需使用@命令,正确写法是@ Square = $i * $i。
修正后的完整代码
#! /bin/csh -f set Square = 0 echo "Please enter a number smaller than 1000" set num = $< if ($num > 1000) then echo "The number is invalid" exit 1 else echo "Square number for 1 to $num" set i = 1 while ($i <= $num) @ Square = $i * $i echo $Square @ i++ end endif
修正后输入455测试,脚本会正常输出1到455的平方数,不再出现while: Expression Syntax错误。
内容的提问来源于stack exchange,提问作者TheQuietlyDepressedCS_Student
相关产品推荐
相关产品推荐

