企业网站样板制作,wordpress怎么加快网站打开速度,wordpress显示访问量,带数据库的网站目录 1.条件结构1.1.文件测试(字符串)1.2.字符串比较1.3.数字条件比较1.4.文件条件判断 2.if多条件判断3.case语句 1.条件结构 测试#xff1a;test 条件 条件为真返回 0#xff0c;条件为假返回 1 语法#xff1a;[ 条件 ] test 条件能够理解以下类型的表达式 1.1.… 目录 1.条件结构1.1.文件测试(字符串)1.2.字符串比较1.3.数字条件比较1.4.文件条件判断 2.if多条件判断3.case语句 1.条件结构 测试test 条件 条件为真返回 0条件为假返回 1 语法[ 条件 ] test 条件能够理解以下类型的表达式 1.1.文件测试(字符串) -n STRING the length of STRING is nonzero -n 字符串的长度 【不是】零成功。 -n不为空案例
[rootlocalhost ~]# vim a.sh
#!/usr/bin/bash
read -p 请输入幸运数字 lucky_num
if [[ -n $lucky_num ]]; thenecho 您的幸运数字是$lucky_num
elseecho 您输入的幸运数字为空exit
fi
//执行
[rootlocalhost ~]# bash a.sh
请输入幸运数字12
您的幸运数字是12
[rootlocalhost ~]# bash a.sh
请输入幸运数字
您输入的幸运数字为空-z STRING the length of STRING is zero -z 字符串长度【是】零成功 对于未定义或赋予空值的变量将是为空串。 -z为空案例为空时则执行
[rootlocalhost ~]# cat b.sh
#!/usr/bin/bash
read -p 请输入幸运数字 lucky_num
if [[ -z $lucky_num ]]; thenecho 您输入的幸运数字为空
elseecho 您输入的幸运数字为$lucky_numexit
fi
//执行
[rootlocalhost ~]# bash b.sh
请输入幸运数字 #不输入留空
您输入的幸运数字为空
[rootlocalhost ~]# bash b.sh
请输入幸运数字23 #输入数字
您输入的幸运数字为23[rootlocalhost ~]# vim string.sh
#!/bin/bash
while :
doread -p 请输入你的密码 passpasswd12345if [[ -z $pass ]];thenecho 您输入的密码不能为空exit 1elseif [[ $pass $passwd ]];thenecho 密码输入成功breakelseecho 您输入的密码有误请重新输入 fifi
done1.2.字符串比较 STRING1 STRING2 等于 the strings are equal STRING1 ! STRING2 不等于 the strings are not equal 1.3.数字条件比较
格式含义-eq(equal)等于-ne(not equal)不等于-ge(Greater than or equal to)大于等于-le(Less than or equal to)小于等于-gt(greater than)大于-lt(less than)小于
1.4.文件条件判断
格式含义-f存在且是正规文件-d存在且是目录-h存在且是符号链接-b块设备-c字符设备-e文件存在
[rootlocalhost ~]# vim file.sh
#!/bin/bash
file/root/test.txt
if [[ -e $file ]];thenecho $file
elseecho 文件不存在 touch $file echo 文件已创建
fi2.if多条件判断
流程控制在一个shell脚本中的【命令执行的顺序】称作脚本的流。大多数脚本会根据一个或多个条件来改变它们的流。 流控制命令能让脚本的流 根据条件而改变的命令称为条件流控制命令 exit语句退出程序的执行并返回一个返回码返回码为0正常退出非0为非正常退出例如: exit 0 条件判断语法if 代码返回0表示真非0为假
if语句语法如下
if [[ -e -eq -ne ]];then //-e -eq -ne是你的测试条件你要测试什么对什么内容做判断例如$? -eq 0list1 //执行list2里面的内容
elif [[ -e -eq ]];then //接着在怎么做。多条件判断list2
else //如果前面的命令没有执行成功那就执行else下面的命令。list3
fi[rootlocalhost ~]# cd /opt/test/script/
[rootlocalhost script]# vim testif.sh
#!/bin/bash
read -p 请输入号码: num
if [[ $num 1 ]];thenecho 1
elif [[ $num 2 ]];thenecho 2
elif [[ $num 3 ]];thenecho 3
else echo 输入有误!
fi
[rootlocalhost script]# bash testif.sh例脚本if.sh必须在脚本后加上适当的参数脚本才能正确执行
[rootlocalhost script]# vim if.sh
#!/bin/bash
if [[ $1 hello ]]; thenecho Hello! How are you ?
elif [[ $1 ]]; thenecho You MUST input parameters
elseecho The only accept parameter is hello
fi
[rootlocalhost script]# chmod x if.sh
//测试
[rootlocalhost script]# ./if.sh
[rootlocalhost script]# ./if.sh hello
[rootlocalhost script]# ./if.sh 434练习 1检测nginx是否已经安装、已经运行如果没有安装和运行则【安装并启动】并【记录启动】的时间保存到日志中。
#!/bin/sh
#check_nginx_install
nginx -V
if [[ $? -ne 0 ]]; thenyum -y install nginx
fi
#check_nginx_status
ps -ef|grep nginx netstat -ntlp|grep nginx
if [[ $? -ne 0 ]]; thensystemctl restart nginx
fi
#record_nginx_up_time
up_time$(systemctl status nginx.service |grep -i active|awk {print $6,$7})
echo $up_time /root/nginx_up_time2测试ip地址主机位从2到100的机器是否存活并把存活的机器记录到文本文件alivehost.txt内。(使用ping命令)
#!/usr/bin/bash
ip192.168.17
for i in {2..100}
doping -c1 -w1 $ip.$i /dev/nullif [ $? -eq 0 ];thenecho $ip.$i is up activehost.txtelseecho $ip.$i is down /dev/nullfi
done多个条件联合 逻辑与前面执行成功后面才执行。前面命令执行失败后面命令也不执行 if [ $condition1 ] [ $condition2 ];then if [[ $condition1 $condition2 ]];then ||逻辑或前面执行失败后面执行前面命令执行成功后面不执行。 if [ $condition1 ] || [ $condition2 ];then if [[ $condition1 || $condition2 ]];then 拓展知识[[ ]]和[ ]的区别 [[ ]]支持正则表达式而[ ]不支持 例如 [[ $a z* ]] # 正则匹配 [ $a z* ] # 无效 [ ] 语法 都可以由 [[ ]] 替代并且后者功能更丰富 [ ] 使用 -a、-o 分别表示与、或 关系 [[ ]]使用 、 ||表示与 、或关系 而且在使用中[[ ]]比[ ]更加的稳定在脚本的使用中建议使用[[ ]] 1. 判断一个用户是否存在
2. 判断当前内核主版本是否为3且次版本是否大于10//主版本获取uname -r|cut -d. -f1 #-d. 以什么为分隔符 -f1打印第一段3//次版本获取uname -r|cut -d. -f210//或者[rootlocalhost ~]# uname -r|awk -F. {print $1}3[rootlocalhost ~]# uname -r|awk -F. {print $2}10
3. 判断vsftpd软件包是否安装如果没有则自动安装 (yum是否能用不能用自动修复安装完成测试下是否能用。)
4. 判断httpd是否运行
5. 判断指定的主机是否能ping通必须使用$1变量
ping -c1 -w1 $1.com3.case语句
case 语句是 shell 中流控制的第二种方式语法如下
case $变量 inpattern1)list1;; //2个英文的分号结尾。pattern2)list2;;... ...patternN)listN;;*) //如果前面命令没有执行成功那么执行下面这个list*;;
esac //结尾第一行声明case关键字调用case语法紧跟的“变量”一般为用户的输入值, in代表从下方的各个模式进行匹配 第2-4行匹配到“pattern1”后进行命令的输出或执行, pattern1: 一般为字符或数值 第11-12行当用户输入的字符不存在匹配模式时, 直接执行或打印)下的命令或语句*
[rootlocalhost script]# vim case1.sh
#!/usr/bin/env bash
case $1 ina)echo one;;b)echo two;;*)echo Usage:$0 {a|b} # $0就是这个脚本;;
esac
//执行结果
[rootlocalhost ~]# bash case.sh
Usage:case.sh {a|b}
[rootlocalhost ~]# bash case.sh a
one
[rootlocalhost ~]# bash case.sh b
two练习 建立脚本case2.sh当执行时要求我们在键盘输入适当的值(数字)当输入正确时并打印当输入错误时会提示你应该输入正确的值。
[rootlocalhost script]# vim case3.sh
#!/usr/bin/env bash
read -p 请输入 get
case $get in
[0-9][0-9])echo -e 你输入的是数字为$get\n;;
*)echo -e 你输入的不是数字。\n;;
esac示例编写系统工具脚本
[rootlocalhost script]# vim system_tools.sh
#!/usr/bin/env bash
#cat -EOF
echo
-------------------------------------------------------------------------
| System_tools V1.0 |
-------------------------------------------------------------------------
| a. Stop And Disabled Firewalld. |
| b. Disabled SELinux Secure System. |
| c. Install Apache Service. |
| d. Quit |
-------------------------------------------------------------------------#EOF
read -t 60 -p 请在1分钟内作出选择 choose
case $choose ina)systemctl stop firewalld systemctl disable firewalld;;b)setenforce 0;;c)yum -y install httpd httpd-tools;;d)exit;;*)printf 请按照上方提供的选项输入\n;;
esac
[rootlocalhost script]# chmod x system_tools.sh
[rootlocalhost script]# ./system_tools.sh练习 建立脚本service.sh当用户执行的时候要求输入(1、2、3、4、5)时安装对应的httpd、vim、wget、更换aliyun yum源等功能当输入错误时会提示你应该输入正确的值。
[rootlocalhost ~]# vim service.sh
#!/bin/bash
echo
-------------------------------------------------------------------------
| System_tools V1.0 |
-------------------------------------------------------------------------
| 1. Install Apache Service. |
| 2. Install Vim. |
| 3. Install Wget. |
| 4. Change Aliyun Yum. |
| 5. Change Tencent Yum. |
| 6. Quit |
-------------------------------------------------------------------------read -t 60 -p 请在1分钟内作出选择 choose
case $choose in1)yum -y install httpd httpd-tools;;2)yum -y install vim;;3)yum -y install wget;;4)yum -y install wgetmv /etc/yum.repos.d/* /tmpwget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repowget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repoyum makecacheyum repolist;;5)yum -y install wgetmv /etc/yum.repos.d/* /tmpwget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repowget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repoyum makecacheyum repolist;;6)exit;;*)echo -e 请按照上方提供的选项输入\n;;
esac[rootlocalhost ~]# bash service.sh
-------------------------------------------------------------------------
| System_tools V1.0 |
-------------------------------------------------------------------------
| 1. Install Apache Service. |
| 2. Install Vim. |
| 3. Install Wget. |
| 4. Change Aliyun Yum. |
| 5. Change Tencent Yum. |
| 6. Quit |
-------------------------------------------------------------------------
请在1分钟内作出选择