、
一、条件语句
if语句结构
单分支
if 判断条件;
then 条件为真的分支代码
fi
双分支
if 判断条件; then
条件为真的分支代码
else
条件为假的分支代码
fi
多分支
if 判断条件1; then
条件1为真的分支代码
elif 判断条件2; then
条件2为真的分支代码
elif 判断条件3; then
条件3为真的分支代码
...
else
以上条件都为假的分支代码
fi
case语句结构
case 变量引用 in
PAT1)
分支1
;;
PAT2)
分支2
;;
...
*)
默认分支
;;
esac
echo -n 表示不换行输出
echo -e 表示输出转义符
常用的转义符
\n | 输出换行 |
---|---|
\r | 光标移至行首,并且不换行 |
\s | 当前shell的名称,如bash |
\t | 插入Tab键 |
\f | 换行,但光标仍停留在原处 |
\ \ | 表示插入"\"本身 |
\b | 表示退格 不显示钱一个字符 |
测试
test 测试文件的表达式 是否成立
格式1:test 条件表达式
格式2:[ 条件表达式 ]
注意[ ]内左右有空格,否则会失败
测试是否成功使用返回值
[ 操作符 文件或目录 ]
操作符:
-d:测试是否为目录(Directory)
-e:测试目录或文件是否存在(Exist)
-a:测试目录或文件是否存在(Exist)
-f:测试是否为文件(File)
-r:测试当前用户是否有权限读取(Read)
-w:测试当前用户是否有权限写入(Write)
-x:测试当前用户是否有权限执行(eXcute)
-L: 测试是否为软连接文件
属性测试补充:
-s FILE #是否存在且非空
-t fd #fd 文件描述符是否在某终端已经打开
-N FILE #文件自从上一次被读取之后是否被修改过
-O FILE #当前有效用户是否为文件属主
-G FILE #当前有效用户是否为文件属组
若真,则状态码变量 $? 返回0
若假,则状态码变量 $? 返回1
[root@localhost data]# ls
09guess.sh case2.sh cname.sh hello.sh ping.sh w99.sh
1.sh case.sh guess_fingers.sh jiecheng.sh qiuhe.sh
99.sh cname guess.sh maopao.sh score.sh
[root@localhost data]# [ -e /data/1.sh ]
[root@localhost data]# echo $?
0
[root@localhost data]# [ -e /data/2.sh ]
[root@localhost data]# echo $?
1
比较整数数值
[ 整数1 操作符 整数2 ] 公式
- -eq:第一个数等于(Equal)第二个数
- -ne:第一个数不等于(Not Equal)第二个数
- -gt:第一个数大于(Greater Than)第二个数
- -lt:第一个数小于(Lesser Than)第二个数
- -le:第一个数小于或等于(Lesser or Equal)第二个数
- -ge:第一个数大于或等于(Greater or Equal)第二个数
[root@localhost data]# a=2
[root@localhost data]# b=3
[root@localhost data]# [ $a -gt $b ]
[root@localhost data]# echo $?
1
字符串比较
常用的测试操作符
- =:字符串内容相同
- !=:字符串内容不同,! 号表示相反的意思
- -z:字符串内容为空
- -n: 字符是否存在
格式
[ 字符串1 = 字符串2 ] 是否相同
[ 字符串1 != 字符串2 ] 是否不相同
[ -z 字符串 ] 是否为空
[ -n 字符串 ] 字符是否存在
[root@localhost data]# str1=wu
[root@localhost data]# str2=hu
[root@localhost data]# [ $str1 = $str2 ]
[root@localhost data]# echo $?
1
[root@localhost data]# [ $str1 != $str2 ]
[root@localhost data]# echo $?
0
[root@localhost data]# [ -z "" ]
[root@localhost data]# echo $?
0
[root@localhost data]# [ -n $str1 ]
[root@localhost data]# echo $?
0
二、循环语句
相对于for,需要知道循环次数
我们只知道停止条件,不知道次数,就需要使用while
直到达到条件
while
当命令判断为假时停止
until
当命令判断为真时停止
[root@localhost test]# vim 1.sh
#!/bin/bash
i=1
sum=0
while [ $i -lt 101 ]
do
sum=$[$sum+$i]
i=$[$i+1]
done
echo $sum
[root@localhost test]# bash 1.sh
5050
三、菜单
[root@localhost test]# vim 2.sh
#!/bin/bash
i=1
sum=0
until [ $i -eq 101 ]
do
sum=$[$sum+$i]
i=$[$i+1]
done
echo $sum
[root@localhost test]# bash 2.sh
5050
vim caidan.sh
#!/bin/bash
PS3="请选择操作:"
menu="
yum仓库
判断用户是否为root
"
select menu in $menu
do
case $REPLY in
1)
cd /etc/yum.repos.d
mkdir old
mv *.repo old
echo "[centos7]
name=ky15
baseurl=file:///mnt
gpgcheck=0
enable=1" > centos7.repo
mount /dev/sr0 /mnt
yum clean all
yum install -y httpd
;;
2)
user=`whoami`
if [ "$user" = "root" ]
then
echo "当前用户是root"
else
echo "当前用户不是root"
fi
;;
3)
echo "结束"
break
esac
done
[root@localhost test]# bash caidan.sh
1)yum仓库
2) 判断用户是否为root
3) 结束
请选择操作:2
当前用户是root
请选择操作:3
结束
[root@localhost test]#
四、函数
格式
函数名(){
命令序列
}
查看函数
[root@localhost ~]# declare -F
declare -f __HOSTNAME
declare -f __SIZE
declare -f __SLAVEURL
删除函数
unset func_name
函数的传参数
#!/bin/bash
sum1 () {
sum=$[$1 + $2]
echo $1
echo $2
echo $sum
}
sum1 $2 $1
[root@localhost test]# bash sum.sh 2 3
3
2
5
递归
[root@localhost data]# vim jiecheng.sh
#!/bin/bash
fact () {
if [ $1 -eq 0 -o $1 -eq 1 ]
then
echo 1
else
echo $[$1 * $(fact $[$1-1])]
fi
}
fact $1
[root@localhost data]# bash jiecheng.sh 5
120