bash shell script fibonacci not showing value 2 after 0 1 1?

馋奶兔 提交于 2019-12-02 17:40:22

问题


I am writing a bash script for fibonacci which is not printing the value after 0 1 1 . It is not printing "2" after 0 1 1. The code is given below.

echo "enter the number"
read n
a=0
b=1
c=0
echo $a
echo $b
while [ $b -le $n ]
do
  c=`expr $a + $b`
  echo $c
  b=`expr $b + 1`
  a=$b
  b=$c
done

回答1:


In bash, do not use the dollar sign on the left hand side of an assignment.

$c=$a+$b

should be

c=$a+$b

but it probably still does not do what you want, try

c=$((a+b))

instead.




回答2:


echo "enter the number"
read n
a=0
b=1
c=0
while [ $b -le $n ]
do
  c=`expr $a + $b`
  echo $c ' = ' $a ' + '  $b
  a=$b
  b=$c
done


来源:https://stackoverflow.com/questions/20175295/bash-shell-script-fibonacci-not-showing-value-2-after-0-1-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!