测试-----test 条件
#条件为真返回 0,条件为假返回 1 #语法------[ 条件 ]
test 能够理解3中类型的表达式
1.文件测试
2.字符串比较
3.数字比较
字符串
-z STRING
the length of STRING is zero
-z 字符串长度。是零成功 #对于未定义或赋予空值的变量将是为空串。
【扩展案例】
# 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 文件或者目录存在
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.测试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
if [ $condition1 ] && [ $condition2 ] [[ $condition1 && $condition2 ]] 与 [ $condition1 -a $condition2 ] 含义相同
shell 分支case语句
```c
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]
case $2 in
foo)
echo "bar"
;;
bar)
echo "foo"
;;
*)
echo "Usage:$0 '{foo|bar}'"
;;
esac
[root@linux-server script]
[root@linux-server script]
练习:建立脚本case.sh,当执行时,要求我们在键盘输入适当的值(one|two|three),当输入正确时并打印,当输入错误 时会提示你,应该输入正确的值。
shell循环-for语句
for i in {取值范围} #for是关键字 i是变量名 in是关键字
do #循环体的开始
循环体
done #循环体的结束
[root@linux-server script]# vim for1.sh
#!/bin/bash
for (( i=1;i <= 5;i++ ))
do
echo "$i"
done
[root@linux-server script]# chmod +x for1.sh
[root@linux-server script]# ./for1.sh
参数解释:
默认值 i=1
条件 i<=多少?取决于定义,为用户输入的变量,先条件成立在执行命令
增幅 i++ 执行一次加一
区别:
i++===先赋值在运算
++i===先运算在赋值
例子
[root@localhost script]# i=1
[root@localhost script]# h=1
[root@localhost script]# let x=i++
[root@localhost script]# echo $x
1
[root@localhost script]# echo $i
2
[root@localhost script]# let y=++h
[root@localhost script]# echo $y
2
[root@localhost script]# echo $h
2
wait:等待上面命令后台执行结束后(即上一个的进程终止),在执行下面的echo命令
参数详解:
seq 打印序列号,只跟数字
seq 命令用于产生从某个数到另外一个数之间的所有整数。
seq命令的原理就不说了,这里说说为什么不能在{ }中使用变量。其实原因写在bash的man手册中:
大意是说,Bash中会最先展开{ }中的内容,这个时候$NUM还不会被具体的值替代,所以是i在循环中读取的是‘{1..$NUM}’的一个完整的字符串,输出时$NUM会被10替代,就有了'{1..10}'这样的结果。
shell 循环while语句
while 条件 #while关键字,条件和if的条件一样,#while循环当条件为真的时候循环同时会一直循环,也就所说的死循环,为假时不循环
do
循环体
done
#注意:while循环处理文件里面的行比较擅长,不管有没有空格都是一行。
案例:
# vim c.sh
#!/usr/bin/bash
i=1
while [ $i -lt 50 ]
do
echo $i
done
注意观察,#请问如何能够自动终止
#在shell中,let命令用于指定算术运算,即 let expr
[root@localhost script]
user1 qfedu123
user2 qfedu456
user3 qfedu567
user4 qfedu789
user5 qfedu012
[root@localhost script]
[ $UID -ne 0 ] && exit 1
while read line
do
user=`echo $line | awk '{print $1}'`
pass=`echo $line | awk '{print $2}'`
id $user &> /dev/null || useradd $user && echo $pass | passwd $user --stdin
done < /opt/test/script/user_pass.txt
[root@localhost script]
[root@localhost script]
案例二:【可返回主菜单】
[root@linux-server script]
while 1>0
do
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")
sed -ri s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
;;
"c")
yum -y install httpd httpd-tools
;;
"d")
exit
;;
*)
echo "请按照上方提供的选项输入!!!"
;;
esac
if [ $? -eq 0 ];then
clear
else
echo "Warning: Your program exist ERROR!!!"
break
fi
done
[root@linux-server script]
[root@linux-server script]
案例三嵌套循环
[root@localhost script]
for i in {1..100}
do
while [ $i -lt 50 ]
do
echo $i
done
done
[root@localhost script]
[root@localhost script]
shell循环until语句
until 条件 #当后面的条件表达式为假的时候的才循环,为真的时候就停止了
do
循环体
done
[root@linux-server script]# cat until.sh
#!/bin/bash
x=1
until [ $x -ge 10 ]
do
echo $x
x=`expr $x + 1`
done
x=1
while [ ! $x -ge 10 ]
do
echo $x
x=`expr $x + 1`
done
[root@linux-server script]# chmod +x until.sh
[root@linux-server script]# ./until.sh
shell 循环控制shift、continue、break、exit
shift命令
#位置参数可以用shift命令左移。比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1、$2、$3丢弃,$0不移动。不带参数的shift命令相当于shift 1。
#测试 shift 命令(x_shift3.sh)
[root@linux-server script]# cat x_shift3.sh
#!/bin/bash
shift
echo "第一个位置参数: $1"
[root@linux-server script]# bash x_shift3.sh 2 3
第一个位置参数: 3
若用户要求 Shell 在不知道位置变量个数的情况下,还能逐个的把参数一一处理,也就是在 $1 后为 $2,在 $2 后面为 $3 等,则需要用shift把所有参数变成$1
#测试 shift 命令(x_shift.sh)
[root@linux-server script]# vim x_shift.sh
#!/bin/bash
until [ $# -eq 0 ]
do
echo "第一个参数为: $1 参数个数为: $#"
shift
done
执行以上程序:
[root@linux-server script]# bash x_shift3.sh 1 2 3 4
结果显示如下:
第一个参数为: 1 参数个数为: 4
第一个参数为: 2 参数个数为: 3
第一个参数为: 3 参数个数为: 2
第一个参数为: 4 参数个数为: 1
从上可知 shift 命令每执行一次,变量的个数($#)减一,而变量值提前一位
#continue、break、exit命令
Linux脚本中的break continue exit
1.break
结束并退出本次循环
2.continue
在循环中不执行continue下面的代码,转而进入下一轮循环
3.exit
退出脚本
常带一个整数给系统,如 exit 0
检测:
[root@localhost script]# vim break.sh
#!/usr/bin/bash
for i in {1..10}
do
if [ $i -eq 7 ];then
continue
#break
#exit 34
else
echo $i
fi
echo "本次输出结束"
done
echo "脚本结束循环"
#可理解为:break是立马跳出循环并退出脚本;continue是跳出当前条件循环,继续下一轮条件循环;exit是直接退出整个脚本