Shell conditional statements Chapter Programming

Test conditions:

1, file test

2, the integer test

3, the logic test string

if statement:

1, if a single branch statement

2, if two-branch statement

3, if a multi-branch statement

4, if nested statements

test command:

Test specific expression is established when the condition is met, the test statement returns 0, otherwise other values.

格式1:test 条件表达式
格式2:[ 条件表达式 ]       //注意有空格

File test:

格式:[ 操作符 文件或目录 ]

Common test operators:

1、-d:测试是否为目录(Directory)
2、-e:测试目录或文件是否存在(Exist)
3、-f:测试是否为文件(File)
4、-r:测试当前用户是否有权限读取(Read)
5、-w:测试当前用户是否有权限写入(Write)
6、-x:测试当前用户是否有权限执行(eXcute)

for example:

方法一:test -d /etc/sysconfig(测试etc目录下是否有sysconfig这个文件或者目录存在)   
              echo $?(进行条件判断,如果由此文件输出结果为0值)
方法二:[ -d /etc/sysconfig ]
              echo $?(进行条件判断,如果无此文件输出结果为非0值,就是1)
Integer comparison:
格式:[ 整数1 操作符 整数2 ]
Common test operators:
1、-eq:等于(Equal)
2、-ne:不等于(Not Equal)
3、-gt:大于(Greater Than)
4、-lt:小于(Lesser Than)
5、-le:小于或等于(Lesser or Equal)
6、-ge:大于或等于(Greater or Equal)

Demo1 :

Objective: Are there any documents we need to see the next directory, if not directly created. Specific operation is as follows:

[root@localhost ~]#  ! test -e /etc/ssc && touch /etc/ssc(查看在etc目录有是否有ssc文件,如果没有直接在此目录下创建)
[root@localhost ~]# cd /etc(验证:进入etc目录下)
[root@localhost etc]# ls(查看详细信息,此时可以找到我们创建的sscw文件)

String comparison:

格式1:[ 字符串1 = 字符串2 ]
​            [ 字符串1 ! = 字符串2 ]
格式2:[ -z 字符串]
I operators commonly used test:
1、=:字符串内容相同
2、!=:字符串内容不同,!表示相反的意思
3、-z:字符串内容为空

Logic test:

格式1:[ 表达式1 ] 操作符 [ 表达式2 ] ...
格式2:命令1 操作符 命令2 ...

Common test operators:

1、-a或&&:逻辑与,“而且”的意思
2、-o或||:逻辑或,“或者”的意思
3、!:逻辑否

1, a single branch structure:

Shell conditional statements Chapter Programming

2, the structure of the bifurcation:

Shell conditional statements Chapter Programming

3, multi-branch structure:

Shell conditional statements Chapter Programming

Application example if statement:

1. Single-branch if statement

2. Dual-branch if statement:

Determine whether the survival of the target host, display test results

Shell conditional statements Chapter Programming

3. multi-branch if statement:

Analyzing score range, excellent separation, qualified and unqualified three block

Shell conditional statements Chapter Programming

Demo2 (judgment result):

Using a single branch to achieve:


vim source.sh
#!/bin/bash
read -p "请输入整数" src
if [ $src -le 50 ]
 then 
    echo "数字太小"
fi
chmod +x source.sh
./source.sh

Be modified to achieve the dual-branch :( into the creation of the executable file)

vim source.sh
#!/bin.bash
read -p "请输入整数" src
if [ $src -le 50 ]
 then 
    echo "数字太小"
 else
    echo "数字太大"
fi

:( achieved using a multi-branch modifications into the creation of the executable file)

vim source.sh
#!/bin.bash
read -p "请输入整数" src
if [ $src -ge 85 ]&& [ $src -le 100 ]
 then 
    echo "$src成绩优秀"
elif [ $src -ge 70 ] && [ $src -le 84]
 then
    echo " $src成绩合格"
 else
    echo "成绩不合格"
fi

Demo3:

Experimental environment: After running race, finals, gender input within 10 seconds, respectively prompted to enter the men's or women's, to determine the use of multiple nested if realized

DETAILED executable file type in the following figure:

Shell conditional statements Chapter Programming

Guess you like

Origin blog.51cto.com/14464303/2439947