2016-09-19 77 views
1

那么,我试图练习一些shell脚本,但我坚持这个while循环excercise。我只是想使用任何数字的使用输入作为循环的条件。如何在while循环中使用用户输入

#!/bin/bash 

a=0 
input="" 
echo "Type any number" 

read $input 

while [$a -lt $input] 
do 
    echo $a 
    a=`expr $a + 1` 
done 
+1

一开始,大部分的脚本是一个字符串内。一旦你注意到了这一点,使用http://shellcheck.net –

回答

2

你可能不知道有这样的脚本:

#!/bin/bash 

a=0 
input="" 
echo "Type any number" #here you forgot to close string with " 

read input #here you don't need $ 


while [ $a -lt $input ] #note extra spaces after [ and before ] 
         #tricky part here is that '[' is a program 
         #and '$a -lt $input ]' are just its params 
         #this is why you need add extra spaces  
do 
    echo $a 
    a=`expr $a + 1` 

done 
+0

谢谢队友,这工作。 –