目录:
1.1 shell中常用运算符 返回顶部
运算符 | 描述 | 示例 |
文件比较运算符 | ||
-e filename | 如果 filename 存在,则为真 | [ -e /var/log/syslog ] |
-d filename | 如果 filename 为目录,则为真 | [ -d /tmp/mydir ] |
-f filename | 如果 filename 为常规文件,则为真 | [ -f /usr/bin/grep ] |
-L filename | 如果 filename 为符号链接,则为真 | [ -L /usr/bin/grep ] |
-r filename | 如果 filename 可读,则为真 | [ -r /var/log/syslog ] |
-w filename | 如果 filename 可写,则为真 | [ -w /var/mytmp.txt ] |
-x filename | 如果 filename 可执行,则为真 | [ -L /usr/bin/grep ] |
filename1 -nt filename2 | 如果 filename1 比 filename2 新,则为真 | [ /tmp/install/etc/services -nt /etc/services ] |
filename1 -ot filename2 | 如果 filename1 比 filename2 旧,则为真 | [ /boot/bzImage -ot arch/i386/boot/bzImage ] |
字符串比较运算符 (请注意引号的使用,这是防止空格扰乱代码的好方法) | ||
-z string | 如果 string 长度为零,则为真 | [ -z "$myvar" ] |
-n string | 如果 string 长度非零,则为真 | [ -n "$myvar" ] |
string1 = string2 | 如果 string1 与 string2 相同,则为真 | [ "$myvar" = "one two three" ] |
string1 != string2 | 如果 string1 与 string2 不同,则为真 | [ "$myvar" != "one two three" ] |
算术比较运算符 | ||
num1 -eq num2 | 等于 | [ 3 -eq $mynum ] |
num1 -ne num2 | 不等于 | [ 3 -ne $mynum ] |
num1 -lt num2 | 小于 | [ 3 -lt $mynum ] |
num1 -le num2 | 小于或等于 | [ 3 -le $mynum ] |
num1 -gt num2 | 大于 | [ 3 -gt $mynum ] |
num1 -ge num2 | 大于或等于 | [ 3 -ge $mynum ] |
1.2 使用if条件语句 返回顶部
1、单分支if语句
#!/bin/bash MOUNT_DIR="/media/cdrom" if [ ! -d $MOUNT_DIR ] then mkdir -p $MOUNT_DIR fi
#!/bin/bash if [ "$USER" != "root" ] then echo "错误:非root用户,权限不足!" exit 1 fi fdisk -l /dev/sda
2、双分支if语句应用
#!/bin/bash ping -c 3 -i 0.2 -W 3 $1 &> /dev/null if [ $? -eq 0 ] then echo "Host $1 is up." else echo "Host $1 is down." fi
[root@localhost ~]# chmod +x pinghost.sh
[root@localhost ~]# ./pinghost.sh 192.168.10.10
Host 192.168.10.10 is up.
[root@localhost ~]# ./pinghost.sh 192.168.10.1
Host 192.168.10.1 is down.
3、多分支if语句应用
#!/bin/bash read -p "请输入您的分数(0-100):" GRADE if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ] then echo "$GRADE 分,优秀" elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] then echo "$GRADE 分,合格" else echo "$GRADE 分,不合格" fi
1.3 shell 中的for循环 返回顶部
1、根据姓名列表批量的添加用户
1)创建用户的列表文件
[root@localhost ~]# vim /root/users.txt
zhangsan
lisi
wangwu
2)编辑批量添加用户的脚本
[root@localhost ~]# vim uaddfor.sh
#!/bin/bash ULIST=$(cat /root/users.txt) for UNAME in $ULIST do useradd $UNAME echo "123456" | passwd --stdin $UNAME &>/dev/null done
[root@localhost ~]# ./uaddfor.sh
[root@localhost ~]# tail -3 /etc/passwd
3)编辑批量删除用户的脚本
[root@localhost ~]# vim udelfor.sh
#!/bin/bash ULIST=$(cat /bbb/users.txt) for UNAME in $ULIST do userdel -r $UNAME &>/dev/null done
1、根据IP地址列表检查主机状态
1)创建IP地址列表文件
[root@localhost ~]# vim /bbb/ipadds.txt
172.16.1.1
172.16.1.111
172.16.1.222
2)编辑循环检查各主机的脚本
[root@localhost ~]# vim chkhosts.sh
#!/bin/bash HLIST=$(cat /bbb/ipadds.txt) for IP in $HLIST do ping -c 3 -i 0.2 -W 3 $IP &> /dev/null if [ $? -eq 0 ] then echo "Host $IP is up." else echo "Host $IP is down." fi done
1.4 shell中的while循环语句 返回顶部
1、批量添加用户脚本
[root@localhost ~]# vim uaddwhile.sh
#!/bin/bash PREFIX="stu" i=1 while [ $i -le 20 ] do useradd ${PREFIX}$i echo "123456" | passwd --stdin ${PREFIX}$i &> /dev/null let i++ done
2、批量删除用户脚本
[root@localhost ~]# vim udelwhile.sh
#!/bin/bash PREFIX="stu" i=1 while [ $i -le 20 ] do userdel -r ${PREFIX}$i let i++ done
3、猜价格游戏
[root@localhost ~]# vim pricegame.sh
#!/bin/bash PRICE=$(expr $RANDOM % 1000) TIMES=0 echo "商品实际价格范围为0-999,猜猜看是多少?" while true do read -p "请输入你猜测的价格数目:" INT let TIMES++ if [ $INT -eq $PRICE ] ; then echo "恭喜你答对了,实际价格是 $PRICE" echo "你总共猜测了$TIMES 次" exit 0 elif [ $INT -gt $PRICE ] ; then echo "太高了!" else echo "太低了!" fi done
1.5 使用case分支语句 返回顶部
1、编写检查用户输入的字符类型的脚本
[root@localhost ~]# vim hitkey.sh
#!/bin/bash read -p "请输入一个字符,并按Enter键确认:" KEY case "$KEY" in [a-z]|[A-Z]) echo "您输入的是 字母." ;; [0-9]) echo "您输入的是 数字." ;; *) echo "您输入的是 空格、功能键或者其他控制字符." esac
2、编写系统服务脚本模板
#!/bin/bash # The next lines are for chkconfig on RedHat systems. # chkconfig: 35 98 02 # description: Starts and stops xxx Server # The next lines are for chkconfig on SuSE systems. # /etc/init.d/xxx # ### BEGIN INIT INFO # Provides: xxx # Required-Start: $network $syslog # Required-Stop: # Default-Start: 2 3 5 # Default-Stop: 0 6 # Short-Description: Starts and stops xxx Server # Description: Starts and stops xxx Server ### END INIT INFO case $1 in start) # 服务启动需要做的步骤 ... ;; stop) # 服务停止需要做的步骤 ... ;; restart) # 重启服务需要做的步骤 ... ;; status) # 查看状态需要做的步骤 ... ;; *) echo "$0 {start|stop|restart|status}" exit 4 ;; esac
3、编写squid服务脚本
[root@s2 ~]# vi /etc/init.d/squid
#!/bin/bash #chkconfig: 2345 90 25 #config: /etc/squid.conf #pidfile: /usr/local/squid/var/run/squid.pid #description: squid - internet object cache. PID="usr/local/squid/var/run/squid.pid" CONF="/etc/squid.conf" CMD="/usr/local/squid/sbin/squid" case "$1" in start) netstat -anpt | grep squid &>/dev/null if [ $? -eq 0 ] then echo "squid is running" else echo "正在启动squid…….." $CMD fi ;; stop) $CMD -k kill &> /dev/null rm -rf $PID &> /dev/null ;; status) [ -f $PID ] &> /dev/null if [$? -eq 0 ] then netstat -anpt | grep squid else echo "squid is not running" fi ;; restart) $0 stop &> /dev/null echo "正在关闭squid……" $0 start &> /dev/null echo "正在启动squid……" ;; reload) $CMD -k reconfigure ;; check) $CMD -k parse ;; *) echo "用法:$0 {start | stop |restart | reload | check | status}" ;; esac
将squid添加为系统服务
[root@s2 init.d]# chmod +x /etc/init.d/squid
[root@s2 init.d]# chkconfig --add squid
[root@s2 init.d]# chkconfig squid on