while循环读取文件
方法一:采用
exc
读取文件,然后进入
while
循环处理
[root@localhost test5]
# cat file
shuju1
shuju2
shuju3
[root@localhost test5]
# cat file.sh
#!/bin/bash
exec < file
while
read a
do
echo
$a
done
[root@localhost test5]
# bash file.sh
shuju1
shuju2
shuju3
方法二:使用
cat
读文件,然后通过管道进入
while
循环处理
cat
file |
while
read line
do
statement1
done
方法三:通过在
while
循环结尾,使用输入重定向方式
while
read line
do
statement1
done
< File
猜商品价格
通过变量
RANDOM
获得随机数价格
提示用户猜测并记录次数,猜中后退出循环
[root@localhost test5]
# vim price.sh
#!/bin/bash
PRICE
=
$[$RANDOM
%
100
]
TIMES
=
0
while
true
do
read
-p
"Please enter the product price [0-99] : "
INT
let TIMES
++
if
[
$INT
-eq
$PRICE
]
then
echo
"Good luck,you guessed it."
echo
"You have guessed
$TIMES
times."
exit
0
elif
[
$INT
-gt
$PRICE
]
then
echo
"
$INT
is too high"
else
echo
"
$INT
is too low"
fi
done
将之前用
for
语句创建的
test01-test30
用户删除
[root@localhost test5]
# vim del_user.sh
#!/bin/bash
i
=
1
while
[
$i
-le
30
]
do
if
[
$i
-le
9
]
then
user
=
test0
$i
else
user
=
test
$i
fi
if
id
-u
$user
&>/dev/null
then
userdel
-r
$user
else
echo
"
$user
is not exists..."
fi
let i
++
done