Shell脚本中的语法错误

时间:2009-12-28 20:55:46

标签: csh shell

编写脚本以读取正整数,然后计算以下序列: 如果数字是偶数,则将其减半 如果它是奇数乘以3并且加1

您应该重复此过程,直到值为1,打印出每个值以及您执行的这些操作的数量。

#! bin\csh

echo "please enter any integer number :) "

set count=0

set number=$<

while($number != 1)

   if($number % 2) then

       @ number = number * 3 + 1 

   else

       @ number = number / 2

   endif

   echo " $number "

   @ count = count ++

end

echo I performed these operations $count times

当我运行脚本时,我收到以下错误: @: Expression Syntax.

2 个答案:

答案 0 :(得分:2)

我相信它的$ variable来获取它的值

$
    Obtains the value of the variable. 

@ var = $a + $x * $z

来源http://www.eng.hawaii.edu/Tutor/csh.html

那就是

@count = $count +1 

#!bin\csh 

让我颤抖

答案 1 :(得分:0)

我得到了正确的解决方案:)

#! /bin/csh

echo "please enter any integer number :) "

set count=0

set inc=1

set number=$<

while($number != 1)

   if($number % 2) then

       @ number = $number * 3 + 1 

   else

       @ number = $number / 2

   endif

   @ count = $count + $inc

   echo " $number "


end

echo I performed these operations $count times