我有一个名为fib.sh的BASH脚本。脚本读取用户输入(数字)并执行计算。我想能打字
$ ./fib.sh 8其中8是输入
目前,我必须等待下一行输入。
$ ./fib.sh
$ 8脚本
#!/bin/bash
read n
a=0
b=1
count=1
fib=$a
while [ $count -lt $n ];
do
fib=$[$a+$b]
a=$b
b=$fib
count=$[$count+1]
done
echo "fib $n = $fib"
exit 0发布于 2013-10-28 15:42:04
因此,您希望向脚本传递一个参数,而不是读取它。在本例中,使用$1,如下所示:
#!/bin/bash
n=$1 <---- this will take from the call of the script
echo "I have been given the parameter $n"
a=0
b=1
count=1
fib=$a
while [ $count -lt $n ];
do
fib=$[$a+$b]
a=$b
b=$fib
count=$[$count+1]
done
echo "fib $n = $fib"
exit 0https://stackoverflow.com/questions/19639072
复制相似问题