我为Fibonacci系列写了一个linux代码(0 1 1 2 3 5 8)但是当我运行它时总是显示我的其他声明。
#!/bin/bash
#This program will show series of Fibonacci numbers upto user input.
echo -n "Enter the number for Fibonacci series: "
read num
if [ $# = 1 ]
then
x1 = 0
x2 = 1
echo "The Fibonacci series for the number $num is: "
for (( y=0;$y<num; y=$y+1 ))
do
echo -n "$x1 "
x2 = $(( $x2 + $1 ))
x1 = $(( $x2 - $x1 ))
done
else
echo "Input is wrong"
fi
如果我删除,如果satement然后我得到第10行和第11行的错误和for循环
答案 0 :(得分:2)
我不确定你为什么要检查参数的数量,但你需要提供一个参数或更改它以检查零参数。你也有一些空间会导致问题。你需要删除它们。最后,正如@bnaecker所提到的,在比较数字相等时,你会想要使用-eq
而不是=
。
#!/bin/bash
#This program will show series of Fibonacci numbers upto user input.
echo -n "Enter the number for Fibonacci series: "
read num
if [ $# = 0 ]
then
x1=0
x2=1
echo "The Fibonacci series for the number $num is: "
for (( y=0;$y<num; y=$y+1 ))
do
echo -n "$x1 "
x2=$(($x2 + $1))
x1=$(($x2 - $x1))
done
else
echo "Input is wrong"
fi