当前位置:网站首页>Basic series of SHEL script (III) for while loop

Basic series of SHEL script (III) for while loop

2022-07-05 07:20:00 jiankang66

  One 、 background

         Although I am a java The programmer , Do the back end , But recently, I often need to read scripts written by others to run projects , So as a back-end programmer , We also need to know shell grammar , Can read some basic shell Script .

Two 、for loop

1、 There are three ways to write , Next, print 1 To 5 As an example

(1)、 The first way to write it , Achieve printing 1 To 5.

#!/bin/bash
for i in 1 2 3 4 5
do 
 echo $i
done

(2)、 The second way , Achieve printing 1 To 5.

for i in {1..5}
do
echo $i
done

(3)、 The third way , Achieve printing 1 To 5.

for((i=1;i<=5;i++))
do
echo $i
done

2、 Some keywords

break

Out of the loop
exit Exit script
continue continue , Jump out of this cycle

3、 give an example , Judge whether it is a prime number

(1)seq The command is used to generate all integers from one number to another

# Enter a number when the command line executes 
read -p " Please enter a number :"  number

# Compare number Is it 1, by 1 Output is not prime , Exit procedure 
[ $number -eq 1 ] && echo "$number  Not prime " && exit

# Compare number Is it 2, by 2 The output is prime , Exit procedure 
[ $number -eq 2 ] && echo "$number  Prime number " && exit


# Ergodic 2 To number-1 Divide by 2 The integer of 
for i in  `seq 2 $[$[$number-1]/2]`
do
        # Compare number Whether the module is 0, by 0 Not prime 
        [ $[$number%$i] -eq 0 ]&& echo "$number  Not prime "  && exit

done
echo "$number Prime number " && exit

4、 For example, create users in batch

(1)for Loop creation 5 Users .

# To etc/group Determine whether to use class The first group 
grep -w ^class /etc/group &>/dev/null
# Judge whether there is a group according to whether the command in the previous step is successful , There is no add group 
test $? -ne 0 && groupadd class
# Traverse creation u1 To u5 user 
for((i=1; i<=5;i++))
do
useradd -G class u$i
# Set the password for the user 
echo 123|passwd --stdin u$i
done

3、 ... and 、while loop

1、 There are two common ways to write , by true perform while loop

(1)、 The first way to write it   [  Judging expressions  ]

while  [ 1 -eq 1]

do

    command

done

(2)、 The second way ,((  Judging expressions  ))

while   (( 1< 2 ))

do

    command

done

2、 give an example ,where Print 1 To 5

i=1
while (($i<=5))
do 

    echo "$i"

    i=$[$i+1]

done


3、 give an example , Script synchronization system time

(1) Every time 30 Second synchronization system time , Alibaba cloud time synchronization server : ntp1.aliyun.com.

(2) Synchronous success , success 100 Send a success notification to your mailbox after times .

(3) Synchronization failure , Send failure notification to mailbox .

(4) We need to pay attention to if There is a space between and conditional judgment .

(5) No, mail command , adopt yum install sendmail install .

#!/bin/bash

NTP=ntp1.aliyun.com
count=0
while true
do
  nptdate $NTP&>/dev/null
  if [ $? -ne 0 ];then
     echo "system date failed"  |mail -s "check system time"  [email protected]
  else
      let count++
      if [ $count -eq 100 ];then
      echo "system date success"  |mail -s "check system time"  [email protected] && count=0
      fi
fi
sleep 1
done

(6) After execution , The results of the email received are as follows .

Four 、 summary

         The above is about shell grammar for loop ,while Everything about the cycle , Hopefully that helped , You can refer to it , If you think it's good , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .

原网站

版权声明
本文为[jiankang66]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140557516058.html