[Linux] 结构化命令 if

语法结构如下:

1. if-then语句

# if-then语句

if command   #根据conmmand的退出状态码,选择执行语句
then
    commands
fi

e.g.
#!usr/bin/bash
testuser=rich
if grep $testuser /etc/passwd
then
        echo The bash files for user $testuser are:
        ls -a /home/$testuser/
fi

2. if-then-else语句

1 #!usr/bin/bash
2 if command     #根据conmmand的退出状态码,选择执行语句
3 then
4     commands
5 else
6     commands
7 fi

3. 嵌套if(elif)

 1 #!usr/bin/bash
 2 if command
 3 then
 4     commands
 5 elif command
 6 then
 7     commands
 8 elif command
 9 then
10     commands
11 else
12     commands
13 fi

4. test命令(单测试条件)

 1 # 主要有3类测试条件
 2     * 数值比较
 3     * 字符串比较
 4     * 文件比较
 5
 6 #usr/bin/bash
 7 if test condition
 8 then
 9     commands
10 fi
11
12 #!usr/bin/bash
13 if [ condition ] # condition前后又空格
14 then
15     commands
16 fi

5. 复合条件测试

 1 # AND测试:必须同时满足两个条件(真)
 2 if [ condition1] && [ condition2 ]
 3 then
 4     commands
 5 fi
 6
 7
 8 #OR测试:满足任何一个条件(任一为真)
 9 if [ conditon1 ] || [ condition2 ]
10 then
11     commands
12 fi

6. if-then高级特性

 1 # 双圆括号:用于数学表达式
 2     (( expression ))
 3
 4 if (( expression ))
 5 then
 6     commands
 7     (( expression ))
 8 fi
 9
10 # 双方括号:用于字符串表达式(e.g. 正则表达式)
11     [[ expression ]]
12
13 if [[ expression ]]
14 then
15     commands
16 fi

7. case命令

 1 # 将指定的变量同不同的模式进行比较
 2
 3
 4 case variable in
 5 pattern1 | pattern2)
 6     commands1;;
 7 pattern3)
 8     commands2;;
 9 *)
10     commands3;;
11 esac
时间: 2024-07-30 01:28:13

[Linux] 结构化命令 if的相关文章

[linux] 结构化命令-for

1 for命令 1 # for:迭代循环:默认空格为分隔符 2 3 for var in list 4 do 5 commands 6 done 1.1 读取列表中的值 #!usr/bin/bash for test in Hello Python Student School do echo The next wprd is $test done echo The last state is $test #一直保持最后迭代的值,除非删除(或更改)它 1.2 读取列表中复杂的值 1 # 使用转义

Linux Shell脚本编写——使用结构化命令(二)

结构化命令中,最基本的类型就是if-then语句 if command then command fi bash shell的if语句会执行if行定义的那个命令,如果命令的退出状态码是0,则代表成功执行,位于then部分的命令就会执行.如果if行定义的命令的退出状态码是其他,则then部分的命令将不会执行,且if-then语句不能测试跟命令的退出状态码无关的条件 代码2-1 [email protected]:/data# cat demo1 #!/bin/bash if date then e

linux shell脚本使用结构化命令

内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: 1 if command 2 then 3 commands 4 fi 这里需要注意的是在其他语言中if 语句之后的对象是一个等式来测试是TRUE还是FALSE值,而在bash shell中if 语句会运行if 行定义那个命令.如果该命令退出码是数字0,则表示该命令运行成功,位于then 后面的命令就会运行.如果退出码是其他值,那么the

第八章 shell学习之循环和结构化命令

for循环 1. 列表for循环 for variable in {list}  #有些像C++/CLR中的for each do ... done 如: 1. [[email protected] tmp]# cat b.sh #! /bin/bash for i in 1 2 3 4 5      #1 2 3 4 5等价于{1..5} do echo $i done [[email protected] tmp]# ./b.sh 1 2 3 4 5 2. [[email protected

Linux Shell脚本编写——使用结构化命令(四)

命令行参数 向shell脚本传数据的最基本方法是使用命令行参数,命令行参数允许在运行脚本时向命令行添加数据值 读取参数 bash shell会将一些称为位置参数的特殊变量分配给命令行输入的所有参数,甚至包括shell执行的程序的名字,$0是程序名,$1是第一个参数,$2是第二个参数 代码4-1 [email protected]:/data# cat demo1 #!/bin/bash factorial=1 for ((i=1;i<=$1;i++)) do factorial=$[ $fact

linux shell脚本使用结构化命令(2)

一.for命令 二.while命令 三.until命令 1.for命令基本格式 1 for var in list 2 do 3 commands 4 done 1 [email protected]:~/testshell> cat fortest.sh 2 #!/bin/bash 3 #test for command 4 5 for city in beijing shanghai shenzhen dalian 6 do 7 echo the city is $city 8 done 9

Linux Shell脚本编写——使用结构化命令(三)

for命令 for var in listdo commandsdone 代码2-1 [email protected]:/data# cat demo1 #!/bin/bash for test in Alabama Alaska Arizona Arkansas "California Colorado" do echo "The next state is $test" done echo "The last state we visited was

《Linux命令行与shell脚本编程大全》第十三章 更多的结构化命令

本章讨论bash shell的循环命令for.while和until 13.1 for命令 重复执行一系列命令在编程中很常见. bash shell提供了for命令,允许你创建一个遍历一系列值的循环.每次迭代都使用其中一个值来执行已定义好的一组命令.下面是基本格式 for var in list do command done 在list参数中需要提供迭代中要用到的一系列值.会依次迭代下去.每次迭代中,var会包含列表中要用到的一系列值. do 和 done直接输入的命令可以是一条或多条标准的b

shell脚本编程-结构化命令2-for命令

1.for命令 (1)语法 for val in list; do commands done  list参数提供了一些列用于迭代的值,val值依次赋值为list中的值,知道list轮询结束. commands可以是一条或多条shell命令,echo $val可以查看当前循环的值 (2)读取列表中的值 $cat test #!/bin/bash # basic for command for test in A B C; do echo the next val is $test done $.