[Linux study notes 31] shell execution flow (for, while, until, if, case, expect, break, continue, exit)

1. for loop


格式:
for 变量 in item1 item2 … itemN
do
  语句1
  语句2
done


写成一行:
for 变量 in item;do 语句1;语句2 done;


The for statement defines four forms of variables

for var in $(seq 起点 终点 步长)
for var in 1 2 3 ... 10
for var in{
    
    1..10}
for((var=0;var<10;var++))
#无限循环
for (( ; ; ))

Exercise 1:

Script: check_host.sh
detects whether 10 hosts and your current host are directly connected to the host is unblocked, if unblocked, the host ip list will be displayed.


#!/bin/bash
for i in {
    
    1..10}
do
	ping -c1 -w1 172.25.254.$i &> /dev/null && {
    
    
		echo " 172.25.254.$i tongchang !"
	} 
done

Insert picture description here

Exercise 2:

Script create.sh

  1. Automatically create users
  2. Add user name file and password file after script
  3. Automatically detect whether the user in the user file exists, and automatically create it if it does not exist, and the password is the content in the corresponding password file

#!/bin/bash
length=`sed -n '$=' user_file`
for LINE in `seq 1 $length`
do
	uName=`sed -n ${
     
     LINE}p $1`
	uPass=`sed -n ${
     
     LINE}p $2`
	id $uName &> /dev/null && {
    
     
		echo "$uName is exist !"
	} || {
    
    
			useradd $uName
			echo $uPass | passwd --stdin $uName &> /dev/null 
			echo "$uName is created !"
		}
done

Insert picture description here

Insert picture description here



2. While loop (condition is true)


while 条件
do
  语句1
  语句2
done


Infinite loop format 1:

while true
do
  语句
done


Infinite loop format 2:

while :
do
  语句
done


3. until loop (condition is false)


The until loop executes a series of commands until the condition is true and stops.
The until loop and the while loop are handled in the opposite way.
Generally the while loop is better than the until loop, but in some cases—and only in rare cases, the until loop is more useful


until 条件
do
  语句1
  语句2
done


4. if


if 条件
then
  语句
elif 条件
then
  语句
else
  语句
fi


(If the else branch has no statement to execute, don't write this else)

Exercise 1:

check_file.sh
detects whether the file exists and determines the file type


#!/bin/bash
while true
do
	read -p "Please input filename:" fileName
	
	if [ "$fileName" == "exit" ]
	then
		exit
	fi

	if [ -e "$fileName" ]
	then
		if [ -f "$fileName" ]
		then
			echo "$fileName is 普通文件 ! "
		elif [ -d "$fileName" ]
		then
			echo "$fileName is 目录 ! "
		elif [ -S "$fileName" ]
		then
			echo "$fileName is 套接字 ! "
		elif [ -L "$fileName" ]
		then
			echo "$fileName is 软链接 ! "
		elif [ -b "$fileName" ]
		then
			echo "$fileName is 块设备 ! "
		elif [ -c "$fileName" ]
		then
			echo "$fileName is 字符设备 ! "
		fi
	else
		echo "$fileName is not exist !"
	fi

done

Insert picture description here



5. case


The case statement is a multiple choice statement. You can use the case statement to match a value and a pattern. If the match is successful, execute the matched command


casein
模式1)
  语句
  ;;
模式2)
  语句
  ;;
*)
  语句
  ;;
esac


(" *) "indicates other circumstances)


Exercise:

Add disk or memory or upload after system_watch.sh, execute as follows:

  1. disk monitor disk usage
  2. memory monitor memory usage
  3. upload monitor start load
  4. Display per second

#!/bin/bash
while true
do
	[ "$1" = "exit" ] && {
    
    
		exit
		}
	case $1 in
	disk|DISK)
		watch -n 1 "df -h "
		exit
		;;
	memory|MEMORY)
		watch -n 1 "free -m"
		exit
		;;
	upload|UPLOAD)
		watch -n 1 "uptime"
		exit
		;;
	esac
done


6. Expect automatic conversation


dnf install expect -y: Install expect

Question script: ask.sh (give execution permission)

#!/bin/bash
read -p "What's your name: " NAME
read -p "How old are you: " AGE
read -p "which class? " CLASS
read -p "How do you feel? " FEEL
echo $NAME is $AGE\'s old study $CLASS fell $FEEL

Answer script 1: answer.exp

#!/usr/bin/expect
spawn /mnt/ask.sh
expect "name"
send "tom\r"
expect "old"
send "18\r"
expect "class"
send "linux\r"
expect "feel"
send "happy\r"
expect eof


Answer script 2: answer.exp

#!/usr/bin/expect
set timeout 2
spawn /mnt/ask.sh

expect {
    
    
"name" {
    
     send "tom\r"; exp_continue }
"old" {
    
     send "18\r"; exp_continue }
"class" {
    
     send "linux\r" ; exp_continue }
"feel" {
    
     send "happy\r" }
}
expect eof


Answer script 3: answer.exp

#!/usr/bin/expect
set NAME [ lindex $argv 0 ]
set AGE [ lindex $argv 1 ]
set CLASS [ lindex $argv 2 ]
set FEEL [ lindex $argv 3 ]

set timeout 2
spawn /mnt/ask.sh

expect {
    
    
"name" {
    
     send "$NAME\r" ; exp_continue }
"old" {
    
     send "$AGE\r" ; exp_continue }
"class" {
    
     send "$CLASS\r" ; exp_continue }
"feel" {
    
     send "$FEEL\r" }
}
expect eof


Answer script 4: answer.sh

#!/bin/bash
echo hello zy !
/usr/bin/expect <<EOF
set timeout 2
spawn /mnt/ask.sh

expect {
"name" { send "$1\r" ; exp_continue }
"old" { send "$2\r" ; exp_continue }
"class" { send "$3\r" ; exp_continue }
"feel" { send "$4\r" }
}
expect eof
EOF


Exercise:

host_list.sh
detects whether the 172.25.254.1-172.25.254.10 network is normally turned on, if the network is normal, please generate a resolution list host_list


#!/bin/bash
EXPECT()
{
    
    
/usr/bin/expect <<EOF
set timeout 2
spawn ssh root@$1 hostname
expect {
"yes/no?" { send "yes\r" ;
    expect "password:" ;
    send "$2\r";
    exp_continue; }
"password:" { send "$2\r" }
}
expect eof
EOF
}

for i in {
    
    1..10}
do
    ping -c1 -w1 172.25.254.$i &> /dev/null
    if [ "$?" = "0" ]
    then
        host_name=`EXPECT 172.25.254.$i westos | tail -n 1`
        grep $host_name /mnt/host_list &> /dev/null || {
    
    
            echo "172.25.254.$i $host_name" >> /mnt/host_list
        }
    fi  
done
sed 's/^M//g' -i /mnt/host_list
#^M为<ctrl>+<V>+<M>

Insert picture description here



7. Break out of the loop (break and continue and exit)


break: Allow to jump out of all loops (terminate all loops after execution)

continue: It will not jump out of all loops, just out of the current loop

exit:drop out

Guess you like

Origin blog.csdn.net/weixin_46069582/article/details/111416105