Shell脚本流程控制(Linux篇)

1.流程控制

流程控制是改变程序运行顺序的指令。

1.1.条件判断

1.1.1.文件类型判断

参数说明
-d判断指定的目录是否存在
-e判断文件是否存在,存在即为真
-f判断普通文件是否存在
-L判断文件是否存在且为连接文件

示例一:

# 判断文件是否存在,存在为0,不存在为1
root@zking:~# test -e a.txt 
root@zking:~# echo $?
0
root@zking:~# test -d a.txt 
root@zking:~# echo $?
1
root@zking:~# test -f b.txt
root@zking:~# echo $?
1
​
# 除了test以外,还可以使用中括号,但是需要注意括号里条件前后必须要用空格
root@zking:~# [-e b.txt]
[-e: command not found
root@zking:~# [ -e b.txt] 
bash: [: missing `]'
root@zking:~# [ -e b.txt ]
root@zking:~# echo $?
1
​
root@zking:~# [ -d /home ] && echo "is directory" || echo "is not directory"
is directory
root@zking:~# [ -f b.txt ] && echo "file" || echo "no file"
no file
root@zking:~# [ -f a.txt ] && echo "file" || echo "no file"
file

1.1.2.文件权限判断

参数说明
-r判断文件是否存在且有读权限
-w判断文件是否存在且有写权限
-x判断文件是否存在且有执行权限,注意:拥有者,所属组,其他人只用有一个有指定权限就算有
-u会返回真,上面的参数也一样 判断文件是否存在且有SUID权限
-g判断文件是否存在且有SGID权限
-k判断文件是否存在且有SBIT权限

示例一:

# 判断a.txt文件是否具备读的权限
root@zking:~# [ -r a.txt ] && echo "yes" || echo "no"
yes
# 判断a.txt文件是否具备写的权限
root@zking:~# [ -x a.txt ] && echo "yes" || echo "no"
no
# 判断paramdemo.sh是否具备执行的权限
root@zking:~# [ -x paramdemo.sh ] && echo "yes" || echo "no"
yes

1.1.3.两个文件的判断

参数说明
file1 -nt file2file1的最后修改时间是否比file2新,是在返回真
file1 -ot file2file1的最后修改时间是否比file2旧,是在返回真
file1 -ef file2file1inode号是否与file2的一致,即是否为同一个文件

示例一:

root@zking:~# touch b.txt
root@zking:~# ls
a.txt  paramdemo02.sh  person.txt   snap
b.txt  paramdemo.sh    readdemo.sh  workspace
# 判断a.txt的最后修改时间是否比b.txt新
root@zking:~# [ a.txt -nt b.txt  ] && echo "yes" || echo "no"
no

1.1.4.整数比较

参数说明
num1 -eq num2==
num1 -ne num2!=
num1 -gt num2>
num1 -lt num2<
num1 -ge num2>=
num1 -le num2<=

示例一:

root@zking:~# [ 1 > 2 ] && echo "yes" || echo "no"
yes
root@zking:~# [ 1 -gt 2 ] && echo "yes" || echo "no"
no

1.1.5.字符串判断

参数说明
-z str是否为空,为空返回真
-n str判断是否为非空,非空返回真
str1 == str2判断是否相等
str1 != str2判断是否不等

示例一:

root@zking:~# name=zs
root@zking:~# [ -z $name ] && echo "yes" || echo "no"
no
root@zking:~# [ -z $age ] && echo "yes" || echo "no"
yes
​
root@zking:~# [ "abc" == "abc" ] && echo "yes" || echo "no"
yes
root@zking:~# [ "abc" == "bdc" ] && echo "yes" || echo "no"
no

1.1.6.多重判断

参数说明
判断1 -a 判断2and
判断1 -o 判断2or
!判断

示例一:

root@zking:~# num=100
root@zking:~# [ -n $num -a $num -gt 200 ] && echo "yes" || echo "no"
no
root@zking:~# num=201
root@zking:~# [ -n $num -a $num -gt 200 ] && echo "yes" || echo "no"
yes

1.2.if语句

if语句格式如下:

if list; then list; [ elif list; then list; ] ... [ else list; ] fi

示例一:

#!/bin/bash
#author test
num=$1
if [ $num -gt 100 ];then
       echo ">100"
fi

示例二:监控磁盘占用率

#!/bin/bash
#author: zking
#description: 当sda1盘的占用达到90%即输出警告信息

# 针对变量初始化
declare -i rate=0
# 获取/dev/sda2的磁盘占用率
rate=$(df -h | grep "/dev/sda2" | awk '{print $5}' | cut -d "%" -f 1)
if [ $rate -ge 10 ];then
        echo "warning! /dev/sda1 is full"
fi

示例三:多if分支

#!/bin/bash
#author: zking
read -p "please input a num:" num
if [ $num -gt 10 -a $num -le 100 ];then
	echo "100>=num>10"
	exit 1
elif [ $num -gt 100 -a $num -le 1000 ]; then
	echo "1000>=num>100"
	exit 2
elif [ $num -gt 1000 -a $num -le 10000 ]; then
	echo "10000>=num>1000"
	exit 3
else
	echo "other num"
fi

1.3.case语句

if elif else语句一样都是属于分支语句,不同点在于,case只能判断一种条件关系,if可以判断多种条件关系。case语句语法格式如下:

case 模式名 in
	模式 1)
		命令
		;;
	模式 2)
		命令
		;;
	*)
		不符合以上模式执行的命令
esac

示例一:

#!/bin/bash
#author: zking
#discription: case
read -p "please input [y/n]:" -t 30 choose

case $choose in
        "y")
                echo "your input y..."
                ;;
        "n")
                echo "your input n..."
                ;;
        *)
                echo "your input is others..."
                ;;
esac

1.4.for语句

for语句命令格式如下:

for 变量名 in 取值列表; do
	命令
done

示例一:

#!/bin/bash
#author: zking
for i in 1 2 3 4 5 6 
do
        echo $i
done

#!/bin/bash
#author: zking
for ((i=0;i<=10;i++))
do
        echo $((i))
done


#!/bin/bash
#author: zking
for i in {1..9}
do
        echo $i
done

#!/bin/bash
#author: zking
list="hello shell world"
for str in $list
do
        echo $str
done

  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值