学习b记 · 第三阶段
一、shell编程
1、数值运算
1)expr
2)$(())
3)$[]
4)let
5)bc
echo $1+$2'='`echo $1+$2 | bc`
echo $1-$2'='`echo $1-$2 | bc`
echo $1*$2'='`echo $1*$2 | bc`
echo $1/$2'='`echo $1/$2 | bc`
echo $1%$2'='`echo $1%$2 | bc`
6)awk
[[ 100 =~ ^[0-9]+$ ]] && echo Y || echo N
-z 长度为0则为真
-n 长度不为0则为真
[ -z $num ] && echo Y || echo N
2、文件判断
[ -f file ]
[ -d dir ]
[ -r file ]
[ -w file ]
[ -x file ]
[ -e file ]
[root@test03 script]
Y
[root@test03 script]
N
3、整数比对
-eq
-ne
-lt
-le
-gt
-ge
[root@test03 script]
[root@test03 script]
/bin/false [OK]
vim /etc/mail.rc
set from=xxxx@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=xxxx@qq.com
set smtp-auth-password=
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
use_disk=`df -h|awk 'NR==2{print $(NF-1)}'`
if [ ${use_disk%\%} -gt 6 ];then
echo "当前磁盘使用率已经高于80%,请及时处理" > /tmp/disk.log
mail -s "`date +%F-%H-%M`_磁盘使用率告警"
else
echo "磁盘使用率正常"
echo "当前磁盘使用正常,请继续摸鱼" > /tmp/disk.log
mail -s "`date +%F-%H-%M`_摸鱼告警"
fi
vim /etc/crontab
30 * * * * root sh /server/scripts/disk.sh &>/dev/null
1、补充一点变量字符串
[root@shell script]
[root@shell script]
www.baidu.com
[root@shell script]
baidu.com
[root@shell script]
www.baidu
[root@shell script]
www.sina.com
2、多整数比较
-a and
-o or
[ 10 -eq 10 -a 100 -gt 20 ] && echo Y || echo N
[ 10 -eq 11 -o 100 -gt 20 ] && echo Y || echo N
1、if
1)语法结构:
if [ 条件表达式 ];then
命令集合
fi
2)if [ 条件表达式 ]
then
命令集合
fi
menu1(){
echo -e "\t\t\t\t\033[35m 1.PHP \033[0m"
echo -e "\t\t\t\t\033[35m 2.Redis \033[0m"
echo -e "\t\t\t\t\033[35m 3.Zabbix \033[0m"
echo -e "\t\t\t\t\033[35m 4.退出 \033[0m"
}
menu2(){
echo -e "\t\t\t\t\033[35m 1.PHP5.4 \033[0m"
echo -e "\t\t\t\t\033[35m 2.PHP7.1 \033[0m"
echo -e "\t\t\t\t\033[35m 3.PHP7.2 \033[0m"
echo -e "\t\t\t\t\033[35m 4.返回上一级 \033[0m"
}
while true
do
menu1
read -p "请输入你要安装的软件编号: " num
if [ $num -eq 1 ];then
menu2
while true
do
read -p "请输入要安装的版本编号[m帮助]: " num1
if [ $num1 = 1 ];then
echo yum instal php5.4 -y
elif [ $num1 = 2 ];then
echo yum instal php7.1 -y
elif [ $num1 = 3 ];then
echo yum instal php7.2 -y
elif [ $num1 = m ];then
clear
menu2
elif [ $num1 = 4 ];then
break
fi
done
elif [ $num -eq 2 ];then
echo yum install redis -y
elif [ $num -eq 3 ];then
echo yum install zabbix -y
elif [ $num -eq 4 ];then
clear
exit
fi
done