Use of shell scripts---while loop

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.