shell(5)shell的执行流控制


1.for循环

用法
for 定义变量
do 使用变量,执行动作
done 结束标志
  • 格式1:命令
    #!/bin/bash
    for WESTOS in seq 1 2 10 #从1到10,每走两步。1,3,5,7,9
    do
    echo $WESTOS
    done

  • 格式2:指定直
    for WESTOS in 1 2 3
    do
    echo $WESTOS
    done

  • 格式3:大括号
    for WESTOS in {10…1}
    do
    echo $WESTOS
    done

  • 格式4:运算
    for ((WESTOS=0;WESTOS<10;WESTOS++))
    do
    echo $WESTOS
    done

脚本练习1:

check_host.sh
用此脚本检测10台与您当前主机直连主机是否网络通常
如果网络通常请显示主机的ip列表

for ip in {1..10}
do
        ping -c1 -w1 172.25.254.$ip &> /dev/null && {
        echo 172.25.254.$ip
        }
done

在这里插入图片描述

脚本练习2:

create_user.sh userfile passfile
cat userfile
user1
user2
user3
cat passfile
user1pass
user2pass
user3pass
用户存在就显示已存在,不存在就建立

[root@node2 mnt]# vim create4_user.sh 
#!/bin/bash
Line_Num=`sed '$=' -n /mnt/userfile` #取文件的行数
for LINE in  `seq 1 $Line_Num`
do
        username=`sed -n ${LINE}p $1`  #按行数循环取该行内容
        password=`sed -n ${LINE}p $2`
        id $username &> /dev/null && {
                echo $username is exist
        }||{
        useradd $username
        echo $password | passwd --stdin $username &> /dev/null && echo $username is created
        }
done

[root@node2 mnt]# sh create4_user.sh userfile passfile
user1 is created
user2 is created
user3 is created

在这里插入图片描述

2.while

用法
while ture #条件为真
do #条件成立所作循环动作
done
%while
[root@node2 mnt]# vim while.sh
#!/bin/bash
while true
do
        read -p "please input word: " WORD
        echo $WORD
done
[root@node2 mnt]# sh while.sh 

3.until

用法
until false ##条件为假
do#条件不成立所作循环动作
done

4.if

用法
if
then
elif
then
else
fi
%if
[root@node2 mnt]# vim if.sh
#!/bin/bash
if [ -e "/mnt/if.sh" ]
then
        echo  yes /mnt/if.sh
elif [ -e "/mnt/file1" ]
then
        echo yes file1
else
        echo no
fi
[root@node2 mnt]# sh if.sh 
yes /mnt/if.sh
脚本练习:

check_file.sh
please input filename: file
file is not exist
file is file
file is direcory
此脚本会一直询问直到用户输入exit为止

%先输入read,写判断退出语句,成立退出,部成立进行判断正文,先判断filename是否存在,不存在说不存在;存在的话再判断是文件还是目录。
[root@node2 mnt]# vim check_file.sh 
#!/bin/bash
while true
do
    read -p "please input filename: " file
    [ "$file" = "exit" ]||[ "$file" = "EXIT" ]&&{
    echo bye
    exit
    }||{
        if [ ! -e "$file" ]
        then
            echo $files is not exist
        elif [ -f "$file" ]
        then
            echo $file is file
        elif [ -d "$file" ]
        then
            echo $file is direcory
        fi
    }
done

[root@node2 mnt]# sh check_file.sh 
please input filename: file1
file1 is file
please input filename: /mnt
/mnt is direcory
please input filename: dd
is not exist
please input filename: exit
bye

%改变table键表示的空格数
[root@node2 mnt]# vim /etc/vimrc 
set ts=4

在这里插入图片描述

5.case

[root@node2 mnt]# vim case.sh
#!/bin/bash
case $1 in
    westos|WESTOS)
    echo linux
    ;;
    linux|LINUX)
    echo westos
    ;;
    *)
    echo error
esac


[root@node2 mnt]# sh case.sh westos
linux
[root@node2 mnt]# sh -x case.sh  #都是只检测一次
+ case $1 in
+ echo error
error
[root@node2 mnt]# sh -x case.sh linux
+ case $1 in
+ echo westos
westos
脚本练习

system_watch.sh disk memory upload (每秒显示)
disk 监控磁盘使用情况
memory 监控内存使用情况
upload 监控启动负载

[root@node2 mnt]# vim system_watch.sh
#!/bin/bash
case $1 in
    disk|DISK)
    watch -n 1 df
    ;;
    memory|MEMORY)
    watch -n 1  free -m
    ;;
    upload|UPLOAD)
    watch -n 1 uptime
    ;;
    *)
    echo error
esac

[root@node2 mnt]# sh system_watch.sh disk
[root@node2 mnt]# vim system_watch.sh
[root@node2 mnt]# sh system_watch.sh memory

在这里插入图片描述

6.expect

  • 1)问题脚本
    #!/bin/bash
    read -p “what’s your name:” NAME
    read -p "How old are you: " AGE
    read -p "Which objective: " OBJ
    read -p "Are you ok? " OK
    echo $NAME is $AGE’s old study $OBJ feel $OK

  • 2)应答脚本
    #!/usr/bin/expect
    set timeout 1
    set NAME [ lindex $argv 0 ]
    set AGE [ lindex $argv 1 ]
    set OBJ [ lindex $argv 2 ]
    set FEEL [ lindex KaTeX parse error: Expected '}', got 'EOF' at end of input: …name" { send "NAME\r";exp_continue }
    “old” { send “KaTeX parse error: Can't use function '\r' in math mode at position 4: AGE\̲r̲";exp_continue …OBJ\r”;exp_continue }
    “ok” { send “$FEEL\r” }
    }
    expect eof

[root@node2 mnt]# vim ask.sh
#!/bin/env bash
read -p "what's your name: " NAME
read -p "how old are you: " AGE
read -p "which class: " CLASS
read -p "are you happy: " FEEL
echo $NAME is $AGE\'s old study  $CLASS feel $FEEL
[root@node2 mnt]# chmod +x /mnt/ask.sh 
[root@node2 mnt]# sh ask.sh 
what's your name: ww
how old are you: 12
which class: linux
are you happy: happ
ww is 12's old study linux feel happ


[root@node2 mnt]# dnf install expect -y
[root@node2 mnt]# vim 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 }
    "happy" { send "happy\r"}
}
expect eof
[root@node2 mnt]# expect answer.exp 
spawn /mnt/ask.sh
what's your name: tom
how old are you: 18
which class: linux
are you happy: happy
tom is 18's old study linux feel happy

[root@node2 mnt]# vim 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 }
    "happy" { send "$FEEL\r"}
}
expect eof

[root@node2 mnt]# expect answer.exp tom 12 linux bye
spawn /mnt/ask.sh
what's your name: tom
how old are you: 12
which class: linux
are you happy: bye
tom is 12's old study linux feel bye

在这里插入图片描述

[root@node2 mnt]# vim answer.sh
#!/bin/env bash
echo hello westos
/usr/bin/expect <<EOF
spawn /mnt/ask.sh
expect {
"name" { send "$1\r" ; exp_continue }
"old" { send "$2\r" ; exp_continue }
"class" { send "$3\r" ; exp_continue }
"happy" { send "$4\r"}
}
expect eof
EOF

[root@node2 mnt]# sh answer.sh 
hello westos
spawn /mnt/ask.sh
what's your name: 
how old are you: 
which class: 
are you happy: 
is 's old study feel
[root@node2 mnt]# sh answer.sh tom 13 c++ bad
hello westos
spawn /mnt/ask.sh
what's your name: tom
how old are you: 13
which class: c++
are you happy: bad
tom is 13's old study c++ feel bad

在这里插入图片描述

脚本练习1

auto_ssh 192.168.0.1 westos
可以自动连接目标主机当目标主机网络不通时报错 显示能ping通的10台主机

脚本练习2

host_list.sh
检测172。25。254。1-172。25。254。10网络是否开启
如果网络正常清生成解析列表hosts_list
格式如下:
ip 主机名称
例如:172。25。254。1为开启状态,主机名为westos_student1.westos.org
hosts_list中
172。25。254。1 westos_student1.westos.org

[root@node2 mnt]# vim host_list.sh
#!/bin/env bash
AUTO_SSH()
{
/usr/bin/expect <<EOF
spawn ssh root@$1 hostname
expect {
"yes/no" { send "yes\r" ; exp_continue }
"password" { send "$2\r" }
}
expect eof
EOF
}

for Host_Num in {1..10}
do
    ping -c1 -w1 172.25.254.$Host_Num &> /dev/null
    if [ "$?" = 0 ]  #如果"$?" = 0 说明上面那条语句成立,执行下面的
    then
        Host_Name=`AUTO_SSH 172.25.254.$Host_Num westos | tail -n 1`
        grep $Host_Name /mnt/hosts_list &> /dev/null || {
            echo "172.25.254.$Host_Num $Host_Name" >> /mnt/hosts_list
            }
    fi
done
sed 's/^M//g' -i /mnt/hosts_list  #^M是ctrl+V再ctrl+M

[root@node2 mnt]# ls
[root@node2 mnt]# cat hosts_list
172.25.254.1 westos_student1.westos.com
172.25.254.2 westos_student2.westos.org
172.25.254.3 westos_student3.westos.org
172.25.254.4 westos_student4.westos.org
172.25.254.5 westos_student5.westos.org
172.25.254.8 westos_student8.westos.org
172.25.254.9 westos_student9.westos.org
172.25.254.10 westos_student10.westos.org

7.break,continue,exit

用法
contiue ##终止当此次前循环提前进入下个循环
break ##终止当前所在语句所有动作进行语句外的其他动作
exit ##脚本退出
[root@node2 mnt]# echo $?  #0 说明上面那条语句成立,其他数字说明部成立
0
[root@node2 mnt]# aaa
bash: aaa: command not found...
[root@node2 mnt]# echo $?
127

#!/bin/env bash
for num in {1..10}
do
    if [ "$num" = "4" ]
    then
        echo luck number
        break
    fi
    echo $num
done
echo end

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qiao_qing/article/details/111578966