shell编程-流程控制
shell编程-条件结构
测试-----test 条件
#条件为真返回 0,条件为假返回 1 #语法------[ 条件 ]
test 能够理解3中类型的表达式
1.文件测试
2.字符串比较
3.数字比较
字符串
-n STRING
the length of STRING is nonzero
-n 字符串的长度 不是零成功。
-z STRING
the length of STRING is zero
-z 字符串长度。是零成功 #对于未定义或赋予空值的变量将是为空串。
STRING1 = STRING2 (等于)
the strings are equal
STRING1 != STRING2 (不等于)
the strings are not equal
# vim string.sh
#!/usr/bin/bash
while :
do
read -p "请输入你的密码: " a
pass=123456
if [ -z $a ];then
echo "您输入的密码不能为空"
exit 1
else
if [ $a = $pass ];then
echo "登录成功"
break
else
echo "您的密码输入有误,请重新输入"
fi
fi
done
#数字
-eq(equal) 等于
-ne(not equal) 不等于
-ge(Greater than or equal to) 大于等于
-le(Less than or equal to) 小于等于
-gt(greater than) 大于
-lt(less than) 小于
#文件
test
-f #存在且是正规文件
-d #存在且是目录
-h 存在且是符号链接
-b 块设备
-c 字符设备
-e #文件存在
案例:
[root@localhost ~]# vim test.sh
#!/usr/bin/bash
file=/opt/test.txt
touch /opt/test.txt
if [ -e $file ];then
echo "$file"
else
echo "文件不存在"
fi
shell分支if语句
流控制:
•在一个shell脚本中的命令执行顺序称作脚本的流。大多数脚本会根据一个或多个条件来改变它们的流。
•流控制命令:能让脚本的流根据条件而改变的命令称为条件流控制命令
•exit语句:退出程序的执行,并返回一个返回码,返回码为0正常退出,非0为非正常退出,例如:
•exit 0
条件判断语法:
if ------代码返回0表示真,非0为假
if语句语法如下:
if [ list1 ];then list1:你的测试条件,你要测试什么,对什么内容做判断
list2
elif [ list3 ];then ---------------> 接着在怎么做。(多条件判断)
list4
else ---------------> 如果前面的命令没有执行成功那就执行else下面的命令。
list5
fi
例:
[root@linux-server ~]# cd /opt/test/script/
[root@linux-server script]# vim testif.sh
#!/bin/bash
read -p "请输入号码: " num
if [ $num = 1 ];then
echo "1"
elif [ $num = 2 ];then
echo "2"
else
echo "输入有误!"
fi
[root@linux-server script]# chmod +x testif.sh
例:脚本if.sh,必须在脚本后加上适当的参数脚本才能正确执行
[root@linux-server script]# vim if.sh
#!/bin/bash
if [ "$1" = "hello" ]; then
echo "Hello! How are you ?"
elif [ "$1" = "" ]; then
echo "You MUST input parameters"
else
echo "The only accept parameter is hello"
fi
[root@linux-server script]# chmod +x if.sh
测试:
[root@linux-server script]# ./if.sh
[root@linux-server script]# ./if.sh hello
[root@linux-server script]# ./if.sh 434
实战:
1)检测apache是否运行,如果没有运行则启动,并记录启动的时间,保存到日志中。
2)测试ip地址主机位从2到100的机器是否存活,并把存活的机器记录到文本文件alivehost.txt内。(使用ping命令)
案例
#!/usr/bin/bash
ip=192.168.198
for i in {2..100}
do
ping -c1 $ip.$i &> /dev/null
if [ $? -eq 0 ];then
echo "$ip.$i is up" >> activehost.txt
else
echo "$ip.$i is down"
fi
done
多个条件联合
&&:逻辑与,前面执行成功,后面才执行。前面命令执行失败,后面命令也不执行
if [ $condition1 ] && [ $condition2 ];then
if [[ $condition1 && $condition2 ]];then
||:逻辑或,前面执行失败,后面执行,前面命令执行成功,后面不执行。
if [ $condition1 ] || [ $condition2 ];then
if [[ $condition1 || $condition2 ]];then
##### shell 分支case语句
```shell
case 语句是 shell 中流控制的第二种方式,语法如下:
case $变量 in
pattern1)
list1
;; ---------------------结尾。
pattern2)
list2
;;
... ...
patternN)
listN
;;
*) --------------------> 如果前面命令没有执行成功那么执行下面这个
list*
;;
esac
命令;;表明流应该跳转到case语句的最后,类似C语言中的break指令。
第一行: 声明case关键字调用case语法, 紧跟的“变量”一般为用户的输入值, in代表从下方的各个模式进行匹配
第2-4行: 匹配到“pattern1”后进行命令的输出或执行, pattern1: 一般为字符或数值
第11-12行: 当用户输入的字符不存在匹配模式时, 直接执行或打印*)下的命令或语句
示例1:
[root@linux-server script]# vim foo.sh
#!/usr/bin/env bash
case $1 in
foo)
echo "bar"
;;
bar)
echo "foo"
;;
*)
echo "Usage:$0 '{foo|bar}'"
;;
esac
[root@linux-server script]# chmod +x foo.sh
[root@linux-server script]# ./foo.sh bar
示例2:
[root@linux-server script]# vim system_tools.sh
#!/usr/bin/env bash
cat <<-EOF
+-------------------------------------------------------------------------+
| System_tools V1.0 |
+-------------------------------------------------------------------------+
| a. Stop And Disabled Firewalld. |
| b. Disabled SELinux Secure System. |
| c. Install Apache Service. |
| d. Quit |
+-------------------------------------------------------------------------+
EOF
echo "Please input your select: " && read var
case "$var" in
"a")
systemctl stop firewalld && systemctl disable firewalld
;;
"b")
setenforce 0
;;
"c")
yum -y install httpd httpd-tools
;;
"d")
exit
;;
*)
printf "请按照上方提供的选项输入!!!\n"
;;
esac
[root@linux-server script]# chmod +x system_tools.sh
[root@linux-server script]# ./system_tools.sh