Linux shell 脚本结构化命令 if-then

shell 脚本结构化命令

1. if-then 语句基本使用

if-then 语句的基本格式:

if command
then
	commands
elif
	commands
else
	commands
fi

bash 的 if 语句会运行 if 后面的命令。如果命令的退出状态码是0(命令运行正确),则运行位于 then 部分的命令。如果是其他值,执行 else 部分的命令。

zzz@ubuntu:~/my_learning$ cat test.sh 
#!/bin/bash


if ls dir
then
    echo "exec then commands"
else
    echo "exec else commands"
fi

zzz@ubuntu:~/my_learning$ ./test.sh 
ls: 无法访问 'dir': 没有那个文件或目录
exec else commands
zzz@ubuntu:~/my_learning$ vim test.sh 
zzz@ubuntu:~/my_learning$ 
zzz@ubuntu:~/my_learning$ cat test.sh 
#!/bin/bash

if ls .
then
    echo "exec then commands"
else
    echo "exec else commands"
fi

zzz@ubuntu:~/my_learning$ ./test.sh 
test.sh
exec then commands
zzz@ubuntu:~/my_learning$ 

2. test 命令

if-then 语句判断的是命令的状态码。如果需要测试状态码以外的条件,可以使用 test 命令。如果 test 判断的条件成立 test 将返回退出状态码0,如果不成立返回非零退出状态码。test 命令的基本格式:

test condition

使用 test 命令测试变量是否包含内容,如果变量不为空执行then部分语句,否则执行else部分语句。

zzz@ubuntu:~/my_learning$ vim test.sh 
zzz@ubuntu:~/my_learning$ cat test.sh 
#!/bin/bash

a="My_Name"
if test $a
then
    echo "exec then commands"
else
    echo "exec else commands"
fi


zzz@ubuntu:~/my_learning$ ./test.sh 
exec then commands
zzz@ubuntu:~/my_learning$ 
zzz@ubuntu:~/my_learning$ 
zzz@ubuntu:~/my_learning$ 
zzz@ubuntu:~/my_learning$ vim test.sh 
zzz@ubuntu:~/my_learning$ cat test.sh 
#!/bin/bash

a=""
if test $a
then
    echo "exec then commands"
else
    echo "exec else commands"
fi

zzz@ubuntu:~/my_learning$ ./test.sh 
exec else commands
zzz@ubuntu:~/my_learning$ 

3. test 命令的另一种测试方法

这种方式,无需声明 test ,而是使用方括号,要注意左括号右边和右括号左边的空格,否则报错。基本格式如下:

if [ condition ]
then
	commands
fi

可以判断的三类条件:

  • 数值比较
  • 字符串比较
  • 文件比较
3.1 数值比较

test 的数值比较只能处理整数,不能处理浮点数。

比较描述
-eq相等
-ge大于等于
-gt大于
-le小于等于
-lt小于
-ne不等于
zzz@ubuntu:~/my_learning$ cat test.sh 
#!/bin/bash

a=4
if [ $a -eq 4 ]
then
    echo "exec then commands"
else
    echo "exec else commands"
fi


zzz@ubuntu:~/my_learning$ ./test.sh 
exec then commands
zzz@ubuntu:~/my_learning$ 

3.2 字符串比较
比较描述
=相等
!=不相等
<小于
>大于
-n str1长度是否非0
-z str1长度是否为0

注意
字符串比较时,需要注意字符串的两个顺序问题:

  1. 大于号和小于号必须使用转义字符 \,否则将被视为重定向符号;
  2. 大于小于顺序和sort命令采用的不同:sort 中大号字母被认为是大于小写字母的;
3.3 文件比较

可以测试Linux文件系统上的文件和目录。

比较描述
-d file是否存在且是目录
-e file是否存在,可以是目录或文件
-f file是否存在且是文件
-r file文件是否存在并可读
-s file文件是否存在并非空
-w file文件是否存在并可写
-x file文件是否存在并可执行
-o file文件是否存在并属当前用户
-G file文件是否存在并且默认组与当前用户相同
file1 -nt file2检查file1是否比file2新
file1 -ot file2检查file1是否比file2旧
3.4 复合条件测试

布尔运算 and , or 。

[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]

4. if-then 高级特性

bash shell 提供了两项可在 if-then 语句中使用的高级特性:

  • 用于数学表达式的双括号;
  • 用于高级字符串处理功能的双方括号;
4.1 双括号
符号描述
val++, val–,++val, --val后增,后减,先增,先减
!, ~逻辑求反,位求反
**幂运算
<<, >>左位移,右位移
&, |位布尔和,位布尔或
&&, ||逻辑和,逻辑或
zzz@ubuntu:~/my_learning$ cat test.sh 
#!/bin/bash

a=4
if (( $a **2 >10))
then
    echo "exec then commands"
else
    echo "exec else commands"
fi


zzz@ubuntu:~/my_learning$ ./test.sh 
exec then commands
zzz@ubuntu:~/my_learning$ 

注:双括号中的大于小于号不需要转义。

4.2 双方括号

双括号中使用 test 测试中的标准字符串比较。另外,双方括号提供了一个特性——模式匹配,可以使用正则表达式。

zzz@ubuntu:~/my_learning$ cat test.sh 
#!/bin/bash

a="my_name"
if [[ $a = *name ]]
then
    echo "exec then commands"
else
    echo "exec else commands"
fi


zzz@ubuntu:~/my_learning$ ./test.sh 
exec then commands
zzz@ubuntu:~/my_learning$ 

5. case 命令

case 命令采用列表格式来检查单个变量的多个值,执行不同的命令。可以通过竖线操作在一行中分隔多个模式。*会捕获所有与已知模式不匹配的值。
基本格式:

case variable in
pattern1) commands;;
pattern2 | pattern3) commands;;
*) default commands;;
esac
zzz@ubuntu:~/my_learning$ cat test.sh 
#!/bin/bash

a="s2"

case $a in

s1 | s2)

    echo "exec s1 or s2 commands";;
s3)
    echo "exec s3 commands";;
*)
    echo "exec * commands";;
esac


zzz@ubuntu:~/my_learning$ ./test.sh 
exec s1 or s2 commands
zzz@ubuntu:~/my_learning$ 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值