如何将用户输入用于while循环

时间:2016-09-19 15:54:56

标签: shell while-loop sh do-while

好吧,我试图练习一些shell脚本,但我坚持这个循环练习。我只是想使用输入的任何数字作为循环的条件。

#!/bin/bash

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

read $input

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

1 个答案:

答案 0 :(得分: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