8.shell中的case判断

格式
     case  变量名 in
        value1)
              command
	      ;;
        value2)
              command
              ;;
        *)
              commond
              ;;
     esac
  • 在case程序中,可以在条件中使用|,表示或的意思, 比如
2|3)
	command
	;;
  • 案例:
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
    echo "Please input a number."
    exit 1
fi

n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
 echo "Please input a number."
 exit 1
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]
then
    tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
    tag=2
elif [ $n -ge 80 ]  && [ $n -lt 90 ]
then
    tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
    tag=4
else
    tag=0
fi
case $tag in
    1)
	echo "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;;
esac:
[root@feature1 ~]# sh -x case.sh
+ read -p 'Please input a number: ' n
Please input a number: aaa
+ '[' -z aaa ']'
++ echo aaa
++ sed 's/[0-9]//g'
+ n1=aaa
+ '[' -n aaa ']'
+ echo 'Please input a number.'
Please input a number.
+ exit 1

[root@feature1 ~]# sh -x case.sh
+ read -p 'Please input a number: ' n
Please input a number: aaa111
+ '[' -z aaa111 ']'
++ echo aaa111
++ sed 's/[0-9]//g'
+ n1=aaa
+ '[' -n aaa ']'
+ echo 'Please input a number.'
Please input a number.
+ exit 1

[root@feature1 ~]# sh -x case.sh
+ read -p 'Please input a number: ' n
Please input a number: 110
+ '[' -z 110 ']'
++ echo 110
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ '[' 110 -lt 60 ']'
+ '[' 110 -ge 60 ']'
+ '[' 110 -lt 80 ']'
+ '[' 110 -ge 80 ']'
+ '[' 110 -lt 90 ']'
+ '[' 110 -ge 90 ']'
+ '[' 110 -le 100 ']'
+ tag=0

[root@feature1 ~]# sh -x case.sh
+ read -p 'Please input a number: ' n
Please input a number: 1000
+ '[' -z 1000 ']'
++ echo 1000
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ '[' 1000 -lt 60 ']'
+ '[' 1000 -ge 60 ']'
+ '[' 1000 -lt 80 ']'
+ '[' 1000 -ge 80 ']'
+ '[' 1000 -lt 90 ']'
+ '[' 1000 -ge 90 ']'
+ '[' 1000 -le 100 ']'
+ tag=0
+ case $tag in
+ echo 'The number range is 0-100.'
The number range is 0-100.

[root@feature1 ~]# sh case.sh
Please input a number: 99
oook

9.for循环

  • seq 用法
[root@feature1 ~]# seq 10
1
2
3
4
5
6
7
8
9
10
[root@feature1 ~]# seq 1 1 10
1
2
3
4
5
6
7
8
9
10
[root@feature1 ~]# seq 1 2 10
1
3
5
7
9
[root@feature1 ~]# seq 2 2 10
2
4
6
8
10
[root@feature1 ~]# seq 10 -2 2
10
8
6
4
2

# -w等宽
[root@feature1 ~]# seq -w 10 -2 2
10
08
06
04
02

  • 语法:for 变量名 in 条件; do …; done
  • 案例1
#!/bin/bash
sum=0
for i in `seq 1 100`
do
    sum=$[$sum+$i]
    echo $i
done
echo $sum
  • 文件列表循环
#!/bin/bash

cd /etc/
for a in `ls /etc/`
do
    if [ -d $a ]
    then
	ls -d $a
    fi
done
[root@feature1 ~]# sh for2.sh
alternatives
audisp
audit
auto.master.d
bash_completion.d
binfmt.d
chkconfig.d
cron.d
cron.daily
cron.hourly
cron.monthly
cron.weekly
dbus-1
default
depmod.d
dhcp
dracut.conf.d
exports.d
firewalld
fonts
gcrypt
gnupg
groff
grub.d
gss
gssproxy
httpd
init.d
iproute2
java
jvm
jvm-commmon
kernel
krb5.conf.d
ld.so.conf.d
libnl
logrotate.d
lvm
maven
modprobe.d
modules-load.d
my.cnf.d
NetworkManager
nginx
ntp
openldap
opt
pam.d
php.d
pkcs11
pki
plymouth
pm
polkit-1
popt.d
postfix
ppp
prelink.conf.d
profile.d
pure-ftpd
python
qemu-ga
rc0.d
rc1.d
rc2.d
rc3.d
rc4.d
rc5.d
rc6.d
rc.d
request-key.d
rpm
rsyslog.d
rwtab.d
sasl2
security
selinux
skel
ssh
ssl
statetab.d
sudoers.d
sysconfig
sysctl.d
systemd
terminfo
tmpfiles.d
tuned
udev
wpa_supplicant
X11
xdg
xinetd.d
yum
yum.repos.d
zabbix

10. while循环

  • 语法 while 条件; do … ; done
  • 案例1 监控脚本 负载大于10 就发邮件
#!/bin/bash
while :
do
    load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
    if [ $load -gt 10 ]
    then
        top|mail -s "load is high: $load" asldkfls@11.com
    fi
    sleep 30
done
[root@feature1 ~]# sh -x while1.sh
+ :
++ head -1
++ awk -F 'load average: ' '{print $2}'
++ cut -d. -f1
++ w
+ load=0
+ '[' 0 -gt 10 ']'
+ sleep 30

  • 案例2
#!/bin/bash
while :
do
    read -p "Please input a number: " n
    if [ -z "$n" ]
    then
	echo "you need input sth."
	continue
    fi
    n1=`echo $n|sed 's/[0-9]//g'`
    if [ -n "$n1" ]
    then
	echo "you just only input numbers."
        continue
    fi
    break
done
echo $n
[root@feature1 ~]# sh while2.sh
Please input a number: aaa
you just only input numbers.
Please input a number: aaa123
you just only input numbers.
Please input a number: 123412
123412

03-11 03:47