2016-09-19 43 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

謝謝隊友,這工作。 –