linux-条件

表达式模板

表达式
[ expression ]
[[ expression ]]
test expression

[ expression ][[ expression ]]记得中括号和表达式必须有空格隔离。

这是语法规定,没有即是语法错误,需要注意。

元素比较

数值比较

符号注释对照说明
-eqequal==等于
-nenot equal!=不等
-gtgreater than>大于
-gegreater equal>=大于等于
-ltless than<小于
-leless equal<=小于等于

字符

符号注释说明
==数值通用相等
!=数值通用不等
-zzero长度为0
-nnot zero长度不为0

不适用数值比较符号,默认是字符操作,通过字符比较也可以进行相等和不等的判断。

通过字面量进行比较而不是数值比较,虽然效果一样,但是具体含义不一致。

文件测试

符号注释说明
-eexist文件存在
-rreadable文件存在且可读
-wwriteable文件存在且可写
-xexecute文件存在可执行
-s文件存在有内容
-ddirectory文件存在为目录
-ffile文件存在且普通
-ccharacter文件存在字符设备
-bblock文件存在块设备

逻辑组合

符号对照说明
````or
&and与运算,两端都必须执行
````
&&and短路与,第一个false马上返回
!not非,逆转逻辑

运算技巧

# 短路或进行语句跳转
ls /arr &> /dev/null || echo "failure"
# 短路与进行语句连接
ls /arr &> /dev/null && echo "success"
执行状态返回真值返回状态
successtrue0(false)
failurefalse1 (true)

python中的andor可能更容易转换。

print( 'a' and 'b' or 'c')
ls /arr &> /dev/null && echo "success" || echo "failure"

条件判断式中,虽然大多数都是进行真值判断。

但是shellpython都一样,能够把语句执行结果当做真值进行转换。

所以,条件连接符也可以当做语句执行判断流程连接操作符。

只是,到底判断的是执行状态还是状态返回码,需要注意,毕竟0是执行成功,却也是false

简单使用

  • 文件信息
#!/usr/bin/bash
filename=$1
if [ -a $filename ];then
    exec ls -lhi $filename
else
    echo "file is not exist: $filename"
fi
  • 关系串联
((!id godme && echo "godme does not exist, add godme" && useradd godme && echo "add godme success") || (echo "godme was existed"))|| echo "modify passwd" && (passwd --stdin godme && echo "modify password success" || echo "modify password failure") || echo "modify password over"
Created with Raphaël 2.2.0 start !id godme echo "godme was existed" echo "modify passwd" passwd --stdin godme echo "modify password success" echo "modify password over" end echo "modify password failure" echo "godme does not exist, add godme" useradd godme echo "add godme success" yes no yes no
echo "a" && echo "b" || echo "c" && echo "d" || echo "e" && echo "f"

打印结果为a b d f

由此可见,||的优先级高于&&, 先计算||然后才是&&

主体逻辑之间需要用()进行分组,以保证逻辑的正确。

(echo "a" && echo "b") || (echo "c" && echo "d") || (echo "e" && echo "f")

a b

  • lu(list user)
#!/bin/bash
cat /etc/passwd | cut -d: -f1
  • uc(user count)
#!/bin/bash
usercount=`wc -l /etc/passwd | cut -d" " -f1`
echo "there are $usercount user"
  • power
#!/bin/bash
[ $# -eq 1 ] && file=$1
[ $# -eq 2 ] && mark=$1 && file=$2
[ -z $mark ] && position='2-10'
[ "$mark" = '-a' ] && position='8-10'
[ "$mark" = '-o' ] && position='2-4'
[ "$mark" = '-g' ] && position='5-7'
echo -n `ls -l $file | cut -d' ' -f1 | cut -c$position`
echo -e "\t $file"
  • 权限查看

./power file

选项含义
-aother
-ggroup
-oown

只能支持一种选项和无选项,复杂的到数组慢慢改。

  • 语句赋值
#!/bin/bash
[ $# -eq 1 ] && first=$1
[ $# -eq 2 ] && first=$1 && second=$2
echo  "first : $first"
echo "second : $second"

这样赋值没问题。

#!/bin/bash
([ $# -eq 1 ] && first=$1) || ([ $# -eq 2 ] && first=$1 && second=$2)
echo  "first : $first"
echo "second : $second"

这样就会失败

#!/bin/bash
([ $# -eq 1 ] && first=$1&& echo "a")|| ([ $# -eq 2 ] && first=$2&&echo "b")
echo $first

这个会打印,但是会任然没有值打印。

这样多测试几回,就会发现一个问题。

赋值语句只有在最后才会生效

#!/bin/bash
[ $# -eq 1 ] && echo " length is 1" && first=$1
[ $# -eq 2 ] && echo " length is 2" && first=$1 && second=$2
echo  "first : $first"
echo "second : $second"

把赋值语句放在最后,这样才能够有值。

赋值放在中间,好比方法内定义的变量,得出结果就会被抹杀。

只有放在最后,才能够保有值。

所以我们在进行逻辑关联的时候,需要明确几点

  • 需要结果还是需要过程
  • 需要过程影响外部数值

一般来说,命令的连接不存在此问题,因为操作始终会得到执行。

对于执行结果的判断,每次都是依据于此来进行判断。

主要的问题就是是否需要把过程的影响反映到外部。

目前遇到的就是赋值的问题。

不过不推荐使用逻辑连接符进行逻辑判断。

可以不用,或者截断。

虽然写的更多,不过更细更清晰。

而且由专门的逻辑判断符号if之类的,所以不必过于纠结。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值