Linux如果 - 其他不工作

时间:2016-03-23 01:15:42

标签: linux bash shell

我为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循环

1 个答案:

答案 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