SHELL脚本学习(二)结构化命令

结构化命令

1、if-then 语句
if command1 #如果不想显示command1的输出信息,可以加上 > /dev/null
then
    ... #如果command1退出码为0,执行这里
elif command2
then
    ... #如果command2退出码为0,执行这里
else
    ... #command1和command2退出码都不是0,执行这里
fi #结束
#elif和else模块可选

例: 查看你是否有ubuntu这个用户,如果有的话显示home/ubuntu目录下的文件信息

#!/usr/bin/bash
user=ubuntu
if grep $user /etc/passwd > /dev/null
then
    ls -l /home/$user
else
    echo  $user not exists
fi

输出:

total 28
-rw-rw-r-- 1 ubuntu ubuntu     0 May 24 15:47 file1
-rw-rw-r-- 1 ubuntu ubuntu    11 May 24 15:49 file2
-rw-rw-r-- 1 ubuntu ubuntu 10240 May 24 15:49 file.tar
drwxrwxr-x 2 ubuntu ubuntu  4096 May 31 12:38 my_shell
-rw-rw-r-- 1 ubuntu ubuntu    18 May 24 11:28 sortnum
drwxrwxr-x 3 ubuntu ubuntu  4096 May 23 20:10 src

把ubuntu 改成ubuntu11 输出

ubuntu11 not exists
2、test命令

if-then 语句只能测试命令的退出状态码,如果想测试其他条件也需要借助 test命令

if test command
then
...
fi

也可以写成

if [ command ] # 方括号和命令之间有空格
then
...
fi

test 命令支持3类条件

  • 数值比较
  • 字符串比较
  • 文件比较
2.1 数值比较
比较描述
n1 -eq n2检查你n1是否等于n2
n1 -ge n2检查你n1是否大于或等于n2
n1 -gt n2检查你n1是否大于n2
n1 -le n2检查你n1是否小于或等于n2
n1 -lt n2检查你n1是否小于n2
n1 -ne n2检查你n1是否不等于n2
#!/usr/bin/bash
first=2
second=5
if test $first -gt $second
then
    echo $first 大于 $second
elif test $first -lt $second
then 
        echo $first 小于 $second
else
    echo $second 等于 $first
fi

输出

2 小于 5
2.2 字符串比较
比较描述
str1 = str2检查str1和str2是否相同
str1 != str2检查str1和str2是否不同
str1 < str2检查str1是否小于str2
str1 > str2检查str1是否大于str2
-n str1检查str1长度是否不为0
-z str1检查str1长度是否为0

< 和 >在使用时需要转义,也就是在前面加转义字符 \,如果不加的话会被当做重定向处理。

例:比较 hello 和 world的大小

str1=hello
str2=world
if [ $str1 > $str2 ]
then
    echo $str1 大于 $str2
elif [ $str2 > str1 ]
then
    echo $str2 大于 $str1
else
      echo $str2 等于 $str1
fi

结果在当前目录下创建了一个名为world的文件,内容为hello。

$ cat world
hello

修正后的代码

...
if [ $str1 \> $str2 ]
...
elif [ $str2 \> str1 ]
...

输出:

world 大于 hello
2.3 文件比较
比较描述
-d file检查file是否存在并且是目录
-e file检查file是否存在
-f file检查file是否存在并且是文件
-r file检查file是否存在并且可读
-s file检查file是否存在并且非空
-w file检查file是否存在并且可写
-x file检查file是否存在并且可执行
-O file检查file是否存在并且属当前用户所有
-G file检查file是否存在并且默认组与当前用户相同
file1 -nt file2检查file1 是否比 file2新
file1 -ot file2检查file1 是否比 file2旧

例:检查目录/home/ubuntu/src是否存在,如果存在则显示所有文件信息

#!/usr/bin/bash
cd_dir=/home/ubuntu/src11
str2=world
if [ -d $cd_dir ]
then
    ls -l $cd_dir
else
      echo $cd_dir not exists
fi

输出:

total 32
-rw-rw-r-- 1 ubuntu ubuntu   560 May 23 20:10 byte_order.cpp
-rw-rw-r-- 1 ubuntu ubuntu    46 May  1 17:46 index
drwxrwxr-x 3 ubuntu ubuntu  4096 May 14 23:37 net
-rwxrwxr-x 1 ubuntu ubuntu 19560 May 23 20:10 sleep_1
3、复合条件测试

if-then 语句支持 AND 和 OR

例:检查src目录下的文件index是否存在,如果存在则显示文件内容

cd_dir=/home/ubuntu/src
str2=world
if [ -d $cd_dir ] && [ -f $cd_dir/index ]
then
    cat $cd_dir/index
else
      echo $cd_dir not exists
fi

输出:

HTTP/1.1 200 OK
Content-Length: 10

1234567890
4、if-then的高级特性
4.1 在子shell中执行命令的单括号
命令格式

(command)

例子
echo $BASH_SUBSHELL
if (echo $BASH_SUBSHELL)
then
    echo subshell
else
    echo not subshell
fi

输出:

0
1
subshell

$BASH_SUBSHELL:子shell的嵌套层级

4.2 用于数学表达式的双括号
命令格式

(( expression ))

符号描述
var++后增
val–后减
++val前增
–val前减
!逻辑取反
~位取反
**幂运算
<<左移
>>右移
&按位与
&&逻辑 AND
||逻辑 OR

双括号内的数学表达式和其他语言(如C/C++)基本一致

例子
#!/usr/bin/bash
var=10
(( var= $var ** 2 ))
if (( $var > 10 )) 
then
    echo $var  \> 45
else
    echo $var \<= 45
fi

输出:

100 > 45
4.3 用于高级字符串处理的双方括号
命令格式

[[ expression ]]

在双方括号中可以使用通配符或正则表达来匹配字符串

例子
#!/usr/bin/bash
str=helloworld
if [[ $str == h* ]]
then
    echo Match successfully
else
    echo Match failure
fi

输出:

Match successfully
case命令
命令格式

case variable in
pattern1|pattern2) command1;;
pattern1) command2;;
*) default commands
esac
  • 30
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值