While loop: Tests a condition repeatedly, as long as the condition is set up and executed repeatedly
The condition can be: test the expression, or the Boolean value true (the condition is always true) and false (the condition is always false)
vi useradd_while.sh # #将下面脚本改写为新建用户的脚本
#!/bin/bash
Pre=stu
I=0
While [$i-le 10];
Do
Useradd $PRE $i
echo 123123 |passwd--stdin $PRE $i
Userdel-r $PRE $i
i=$ (expr $i + 1)
Let i++
Done
: Wq
1. function random, let, exit:
Random is a function of generating a stochastic number
Echo $RANDOM # #生成随机数
Ehco $RANDOM% # #获取100内的随机数
echo $RANDOM% # #获取1000内的随机数
The Let command acts as a self-decrement operation for a variable:
I=1
Let i++
Echo $i
Let i++
Echo $i
Let i--
Echo $i
Expr $i + 1 #体会体会
Comprehensive application: Define a random value for true, you lose the right to stop and see how many times you can lose
VI banji.sh
#/bin/bash
p=$ (expr $RANDOM% 1000)
T=0
echo "Please insert a number like this (1-999)"
While True;do
Read-p "Please give number:" INT
Let t++
If [$INT-eq $P];then
echo "Your luckly. Right! "
Echo $T
Exit 0 # #当你输对了则退出程序脚本
elif [$INT-gt $P];then
echo "Too High"
Else
echo "Too Low"
Fi
Done
: Wq
2, distinguish Exit,break
Exit quit the program (script), break just exits the loop body
VI while.sh
#!/bin/bash
I=0
j=$ (expr $RANDOM% 10)
While True;do
Echo $i
Let i++
Sleep 1 # #休眠1秒避免死循环产生, can be removed try
If [$i-eq $j];then
echo "OK, $j random"
Break # #将break替换成exit看看效果
Fi
Done
Df-ht
: Wq
This article is from the "Lp-linux" blog, make sure to keep this source http://linuxlp.blog.51cto.com/11463376/1774157
Use of shell scripts---while loop