shell (3) Relevant expression

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wzj_110/article/details/100154553

A file expression

Conventional

# 特殊-->e是不是一般文件!

if [ -f  file ]    如果文件存在(普通文件)
if [ -d ...   ]    如果目录存在(directory)
if [ -s file  ]    如果文件存在且非空(socket)
if [ -r file  ]    如果文件存在且可读(可读)
if [ -w file  ]    如果文件存在且可写(可写)
if [ -x file  ]    如果文件存在且可执行(可执行)

Help Documentation : man test

root: not affected by permissions, keep this in mind!

Requirement 1 : file exists and is a regular file output 1

[ -f temp ] && echo 1 || echo 0

Involved : simple logic control!

Requirement 2 : Determine permission

[root@server1 mnt]# chmod 000 temp
[root@server1 mnt]# ll temp 
---------- 1 root root 11 Aug 29 12:29 temp
[root@server1 mnt]# [ -r temp ] && echo 1 || echo 0
1

Description : Incomplete Look permission to see the end result, for root privileges sometimes does not work!

Demand 3 : shell variable test

[root@server1 mnt]# file1=/etc/rc.local 
# 是不是文件!-->最好加上双引号,表示一个整体!
[root@server1 mnt]# [ -f "$file1" ] && echo 1 || echo 0
1

# 对单个文件或目录变量的测试需要加双引号,避免错误

4 requirements : a plurality of write commands {} in

#!/bin/bash
[ $1 -eq 8 ] || {
        echo 1
        echo 2
        echo 3
}

# 条件表达式判断条件后面执行多条命令语句写法

Description : a plurality of discharge command with {}, {} indicates that the following command is a whole!

Conditional Expressions Features : with respect to conditional expressions, simple, short and pithy!

The relevant comparison

1、test 测试表达式

2、[ 测试表达式 ] #两边需要有空格

3、[[ 测试表达式 ]]

4、(( 测试表达式 )) # 无需空格,常规数学符号!

说明:第一种和第二种是等价的,第三种是扩展的test命令(扩展的地方),语法4常用于计算

在[[]] 双中括号中可以使用通配符等进行模式匹配,&& ||  >  < 等操作符可以直接应用于双中括号中,但不能用于单中括号中

Two    string expression

-n  nonzero       ---->长度是不是非0
-z  zero          ---->字符串的长度是不是非0

"串1" = "串2"     ---->两个字符串是不是相等
"串1" != "串2"   ---->两个字符串是不是不相等

# 注意1:所有的字符串测试操作必须用""引起来!
# 注意2:比较符号的两端必须有空格

Space problems caused

[root@server1 mnt]# [ "wzj"="1" ]&& echo 1 || echo 0
1
[root@server1 mnt]# [ "wzj" = "1" ]&& echo 1 || echo 0
0

Related exercises

# 以"wzj"载体

[root@server1 mnt]# [ -n "wzj" ] && echo 1 || echo 0
1
[root@server1 mnt]# [ -z "wzj" ] && echo 1 || echo 0
0
[root@server1 mnt]# [ ! -z "wzj" ] && echo 1 || echo 0  # 取反
1

Description : About omitted variables !

Note : If the variable does not exist (undefined), the default length of 0!

Related imitate

# 定位-->sed -n "30,31p" /etc/init.d/network

vim /etc/init.d/network # 提供的例子

[ "${NETWORKING}" = "no" ] && exit 6

Three    integer expression

Features: text digitally compare!

Note : different scenarios using different symbols!

Extended : If mixed, to carry out special symbol (<>) in [] in comparison requires backslash \ transfer!

Conclusions

Description of 2

[root@server1 mnt]# [[ 2 > 11 ]] && echo 1 || echo 0
1
# [[]]逻辑比较(第一位)

 

Guess you like

Origin blog.csdn.net/wzj_110/article/details/100154553