新秀篇 ##Linux中shell中的常用语句##

一.exit,break,continue三种语句的不同:
exit 完全中断脚本的执行
break 中断脚本循环,对循环以外的语句没有影响
continue 退出本次循环,进行下一次循环
【实验】:

[root@localhost mnt]# vim file.sh                   ##编辑脚本
[root@localhost mnt]# cat file.sh
#!/bin/bash
for NUM in {1..5}
do 
        if
        [ "$NUM" -eq  3 ]
        then
                           $1
         fi
        echo $NUM
done
echo hello westos!!!
[root@localhost mnt]# sh  file.sh                   ##执行脚本
1
2
3
4
5
hello westos!!!
[root@localhost mnt]# sh  file.sh exit              ##加exit语句执行脚本
1
2
[root@localhost mnt]# sh  file.sh  break             ##加break语句执行脚本   
1
2
hello westos!!!
[root@localhost mnt]# sh  file.sh  continue         ##加continue语句执行脚本
1
2
4
5
hello westos!!!

二.for语句:
用法一:for NUM in 1 2 3 或者 for NUM in {1..3}

[root@localhost mnt]# vim for.sh
[root@localhost mnt]# cat for.sh
#!/bin/bash
for NUM in {a..f}
do
           echo $NUM
done
[root@localhost mnt]# sh for.sh 
a
b
c
d
e
f

用法二:for NUM in seq 1 3 或者 for NUM in seq 1 2 5

[root@localhost mnt]# vim for.sh
[root@localhost mnt]# cat for.sh
#!/bin/bash
for NUM in `seq 1 2 5`                         ##步距为2,即每隔2执行命令
do
           echo $NUM
done
[root@localhost mnt]# sh for.sh 
1
3
5

【实验:编写脚本check_host.sh检查1-50的主机是否可以连通】:

[root@localhost mnt]# cat check_host.sh             ##编辑脚本
#!/bin/bash
for IP in {1..50}                             ##从1到50循环
do
  ping -c1 -w1 172.25.254.$IP &> /dev/null && echo 172.25.254.$IP is up
done
[root@localhost mnt]# sh check_host.sh                           ##执行脚本
172.25.254.1 is up
172.25.254.2 is up
172.25.254.3 is up
172.25.254.4 is up
172.25.254.5 is up
172.25.254.6 is up
172.25.254.7 is up
172.25.254.8 is up

三.while语句:
while true
do
echo -n uptime>/dev/ D e v t t y e c h o n e \r \r / d e v / Dev_tty
sleep 2
done
1.更新进程:
方法一:

[root@localhost mnt]# cat test.sh
#!/bin/bash
while true
do
                       uptime
                       sleep 1
done
[root@localhost mnt]# sh test.sh
 04:42:33 up  7:46,  2 users,  load average: 0.00, 0.01, 0.05
 04:42:34 up  7:46,  2 users,  load average: 0.00, 0.01, 0.05
 04:42:35 up  7:46,  2 users,  load average: 0.00, 0.01, 0.05
 04:42:36 up  7:46,  2 users,  load average: 0.00, 0.01, 0.05
^C                                ##一行一行显示进程太麻烦 

方法二:

[root@localhost mnt]# vim test.sh
[root@localhost mnt]# cat test.sh
#!/bin/bash
while true
do
             echo -n  `uptime`
             echo -ne "\r    \r"
             sleep 1
done
[root@localhost mnt]# sh test.sh
     6:45 up 7:50, 2 users, load average: 0.00, 0.01, 0.05   ##每一秒只在一行进行更新进程

2.编辑脚本监控设备达到80%时给root发送邮件:

[root@localhost mnt]# sh use.sh
#!/bin/bash
Use_Memory=`df | awk -F " " '/\/$/{print $5}' | awk -F "%" '{print $1}'`
while true
do
       [ "$Use_Memory" -gt "80" ] && {
        echo "Your / will full !!" |mail -s warning root
       }
        sleep 1
done

四.if语句:
if
then
elif
then
…..
else
fi
1.编写一个脚本CHECK_fil.sh 显示文件存在时它的类型,不存在时报错:

#!/bin/bash
Check_file()
{
     if
     [ "$1" "$2"]
     then
     echo "$2" is $3                 ##$3输出的是第三列文件的类型
     exit 0
     fi
}

if
[ "$#" -ne  "1"]               ##"$#"与“1”是否不一致
then
     echo "please input a file follow script!!"         ##如果不一致,需要给定一个文件名
elif
[ ! -e "$1"]                                               ##判断“$1”是否存在
then
     echo "$1 is not exist !!"                            ##不存在则报错
else
        Check_file -L $1 link                             ##存在则判断文件类型
        Check_file -f $1 "common file"
        Check_file -S $1 socket
        Check_file -b $1 block
        Check_file -c $1 character
        Check_file -d $1 directory
fi                                                                                                        1,1           Top

2.脚本:用户
1.文件数量不对报错
2.文件不存在
3.文件行数差异
4.用户存在显示用户存在,但是不改变此用户密码
5.当用户不存在建立用户并设定相应密码

#!/bin/bash
if
[ "$#" -ne "2" ]
then
        echo -e "\033[31m1.ERROR:please input userfile and passwordfile\033[0m"
        exit 1
elif
[ ! -e "$1" ]
then
        echo -e "\033[31m2.ERROR: $1 is not exit!!\033[0m"
        exit 1
elif
[ ! -e "$2" ]
then
        echo -e "\033[31m3.ERROR: $2 is not exit!!\033[0m"
        exit 1
elif
USERFILE_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
PASSFILE_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $2`
[ "$USERFILR_LINE" -ne "$PASSFILE_LINE" ]
then
        echo "4.$1's line ia different from $2's"
        exit 1
fi

五.case语句:
1.脚本:写cat显示dog,写dog显示cat:

[root@localhost mnt]# vim test1.sh
#!/bin/bash
case $1 in
    dog)
    echo cat
    ;;
    cat)
    echo dog
    ;;
    *)
    echo error
esac
[root@localhost mnt]# sh -x test.sh dog 
[root@localhost mnt]# sh -x test.sh cat   ##都为一次

六.expect 自动应答语句:
1.脚本:编写可登录IP

[root@localhost mnt]# vim auto_connect.exp
#!/usr/bin/expect 
set timeout 5
set IP [ lindex $argv 0 ]
set PASS [ lindex $argv 1 ]
spawn ssh root@$IP  
expect {
        "yes/no" { send "yes\r";exp_continue }
        "password" { send "$PASS\r" }
}
interact

2.脚本:查看可登录的IP

[root@localhost mnt]# vim check_ip.sh
#!/bin/bash
AUTO_CONNECT{
/usr/bin/expect<<EOF
set timeout 1
spawn ssh root@$172.25.254.$IP_NUM hostname  
expect {
    "yes/no" { send "yes\r";exp_continue }
    "password" { send "westos\r" }
}
expect eof
EOF
}
for IP_NUM in {1..10}
do
        ping -c1 -w1 172.25.254.$IP_NUM &> /dev/null && {
    Host_Name=`AUTO_CONNECT|grep -E "authenticity|fingerprint|ECDSA|connecting|Warning|password|spawn " -v`
}
    echo $Host_Name 172.25.254.$IP_NUM >> /mnt/hostname
done
[root@localhost mnt]# sh check_ip.sh
[root@localhost mnt]# cat /mnt/hostname
[root@localhost mnt]# vim /mnt/hostname
里面有^M(ctrl+^和ctrl+M)    将它替换:%s/^M/ /g

猜你喜欢

转载自blog.csdn.net/china_zgd/article/details/80759789