Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux,如果其他系统不工作_Linux_Bash_Shell - Fatal编程技术网

Linux,如果其他系统不工作

Linux,如果其他系统不工作,linux,bash,shell,Linux,Bash,Shell,我为Fibonacci系列0 1 2 3 5 8编写了一个linux代码,但当我运行它时,它总是显示我else语句 #!/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 Fibo

我为Fibonacci系列0 1 2 3 5 8编写了一个linux代码,但当我运行它时,它总是显示我else语句

#!/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

如果我删除if语句,那么我得到了第10行和第11行以及for循环的错误

我不确定为什么要检查参数的数量,但是您需要提供一个参数,或者将其更改为检查零参数。还有一些空间会导致问题。你需要把它们去掉。最后,正如@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

您应该使用-eq,它以代数方式比较整数用于字符串比较。查看人工测试了解更多详细信息。我不理解开始时的逻辑。命令行上预期的参数是什么?它和为num计算fib有什么关系呢?预期的参数是数字,比如4,5,6等等,循环运行到那个数字,生成那个么多的数字。例如,如果用户输入4,则用户将获得4个fib编号,这可能有效,但逻辑仍然存在固有缺陷。}@叮叮,没错,但在回答这样的问题时,我更倾向于把我的答案减到最少。有了经验,程序员往往会自己改进并从错误中学习。@TylerCrompton:我不是在批评你的答案,而是对它的反应