Linux-Shell脚本编程-学习-5-Shell编程-使用结构化命令-if-then-else-elif
2016-09-19 15:29
1041 查看
if-then语句
if-then语句格式如下
if comman
then
command
fi
bash shell中的if语句可鞥会和我们接触的其他if语句的工作方式不同,bash shell的if语句会运行if语句后面的那个命令,如果该命令的退出状态码是0 那么执行位于then部分的的命令。
代码实例
#!/bin/bash
#if-then test
if date
then
echo "this is the if-then test"
fi
这个脚本的功能就是,执行date命令,如果date命令执行成功,那么他的退出状态码是0就执行then后面的代码,在屏幕上面输一行文字,this is the if-then test
then后面的语句可以是一条或多条,和我们写简单的shell脚本没有区别,这里会一条一条的执行下去
测试实例代码
#if-then test
if date
then
echo "this is the if-then test"
testusername=dreamlife
if grep $testusername /etc/passwd
then
如果我们想要在shell和我们平常一样使用if else的功能,我们需要使用一下命令格式
if command
then
command
else
command
这里,如果 执行if后面的命令的退出状态码是0 就执行then后面的代码块,否则就执行else后面的代码块
测试代码
#!/bin/bash
#if-then-else
testuser=dreamlife
if grep $testuser /etc/passwd
then
echo 'the files for user $testuser are:'
ls -a /home/$testuser/ *
else
echo "the user name $testuser dose not exist on this system"
fi
在shell编程中,也是有if嵌套的,使用格式如下
if command1
then
command1-set
elif command2
then
command-set2
elif command3
then
command3-set
...
fi
这个就没有实例代码了,如果有兴趣的,可以吧上面的代码改吧改吧试试看,每次只能测试一种。
好了,学习下一个命令,test
test是个好东西,他的功能之一就是可以是我们shell的if可以比较真假的,test的基本命令格式很简单
test conditioncondition是test命令要测试的一系列参数和值,当用在if-then语句的时候,test命令执行,如果tets命令中列出的条件为真的时候,退出状态码为0 否则为1,这样就可以在if-then中使用了
就是下面的格式了
if test condition
then
commands
fi
不过每次这么写也挺别扭的。所以,在bash中,提供了另外一种tets的写法,那就是方括号[]
不过必须要在左方括号右面,右方括号左面各加一个空格才可以,不然报错
if [ condition ]
then
commands
fi
test命令可以判断三种类型条件
1. 数值比较
2. 字符串比较
3. 文件比较
第一类,test数值比较的基本功能
1. 检查n1是否与n2相等:n1 -eq n2
2. 检查n1是否大于或等于n2:n1 -ge n2
3. 检查n1是否大于n2:n1 -gt n2
4. 检查n1是否小于或等于n2:n1 -le n2
5. 检查n1是否小于n2:n1 -lt n2
6.检查n1是否不等于n2:n1 -ne n2
测试用例
#!/bin/bash
#using numeric test comparisons
var1=10
var2=11
if [ $var1 -gt 5 ]
then
echo "the test value $var1 ia greater than 5"
fi
if [ $var1 -eq $var2 ]
then
echo "the values are equal"
else
echo "the calues are different"
fi
这里注意的是,浮点数不能比较。下面是错误例子
#!/bin/bash
#testing floating point numbers
val1=`echo "scale=4;10/3" | bc`
echo "the test values is $val1"
if [ $val1 -gt 3 ]
then
echo "the result if larger than 3"
fi
这里bash提示需要整数表达式
第二类 字符串的比较
1. 检查str1是否和str2相同:str1 = str2
2. 检查str1是否和str2不同:str1 != str2
3. 检查str1是否比str2小:str1 <str2
4. 检查str1是否比str2大:str1 >str2
5. 检查str1的长度是否为非零:-n str1
6. 检查str1的长度是否为0:-z str1
下面是实例代码
#!/bin/bash
#testing string equality
testuser=dreamlife
if [ $USER = $testuser ]
then
echo "Welcom $testuser"
fi
#!/bin/bash
#testing string equality
testuser=dreamlife
if [ $USER != $testuser ]
then
echo "this user is not $testuser"
else
echo "Welcom $testuser"
fi
在字符串比较的时候,需要注意两个问题,
1. 大于小于符号必须使用转义,否则shell会把他们当做重定向符号而把字符串当做文件名
2. 大于小于顺序和sort命令所采用不同
第一个问题
#!/bin/bash
#mis-using string comparisons
val1=baseball
val2=hockey
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
第二个儿问题
#!/bin/bash
#testing string sort order
val1=testing
val2=Testing
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
字符串大小
#!/bin/bash
#testing string length
val1=testing
val2=''
if [ -n "$val1" ]
then
echo "the string '$val1' is not empty"
else
echo "the string '$val1' is empty"
fi
if [ -z "$val2" ]
then
echo "the string '$val2' is empty"
else
echo "the string '$val2' is not empty"
fi
后面还有文件的比较,由于文件比较内容比较多,我会在写一个。
if-then语句格式如下
if comman
then
command
fi
bash shell中的if语句可鞥会和我们接触的其他if语句的工作方式不同,bash shell的if语句会运行if语句后面的那个命令,如果该命令的退出状态码是0 那么执行位于then部分的的命令。
代码实例
#!/bin/bash
#if-then test
if date
then
echo "this is the if-then test"
fi
这个脚本的功能就是,执行date命令,如果date命令执行成功,那么他的退出状态码是0就执行then后面的代码,在屏幕上面输一行文字,this is the if-then test
then后面的语句可以是一条或多条,和我们写简单的shell脚本没有区别,这里会一条一条的执行下去
测试实例代码
#if-then test
if date
then
echo "this is the if-then test"
testusername=dreamlife
if grep $testusername /etc/passwd
then
echo "the bash file for user $testusername art:" ls -a /home/$testusername/.b* fi fi
如果我们想要在shell和我们平常一样使用if else的功能,我们需要使用一下命令格式
if command
then
command
else
command
这里,如果 执行if后面的命令的退出状态码是0 就执行then后面的代码块,否则就执行else后面的代码块
测试代码
#!/bin/bash
#if-then-else
testuser=dreamlife
if grep $testuser /etc/passwd
then
echo 'the files for user $testuser are:'
ls -a /home/$testuser/ *
else
echo "the user name $testuser dose not exist on this system"
fi
在shell编程中,也是有if嵌套的,使用格式如下
if command1
then
command1-set
elif command2
then
command-set2
elif command3
then
command3-set
...
fi
这个就没有实例代码了,如果有兴趣的,可以吧上面的代码改吧改吧试试看,每次只能测试一种。
好了,学习下一个命令,test
test是个好东西,他的功能之一就是可以是我们shell的if可以比较真假的,test的基本命令格式很简单
test conditioncondition是test命令要测试的一系列参数和值,当用在if-then语句的时候,test命令执行,如果tets命令中列出的条件为真的时候,退出状态码为0 否则为1,这样就可以在if-then中使用了
就是下面的格式了
if test condition
then
commands
fi
不过每次这么写也挺别扭的。所以,在bash中,提供了另外一种tets的写法,那就是方括号[]
不过必须要在左方括号右面,右方括号左面各加一个空格才可以,不然报错
if [ condition ]
then
commands
fi
test命令可以判断三种类型条件
1. 数值比较
2. 字符串比较
3. 文件比较
第一类,test数值比较的基本功能
1. 检查n1是否与n2相等:n1 -eq n2
2. 检查n1是否大于或等于n2:n1 -ge n2
3. 检查n1是否大于n2:n1 -gt n2
4. 检查n1是否小于或等于n2:n1 -le n2
5. 检查n1是否小于n2:n1 -lt n2
6.检查n1是否不等于n2:n1 -ne n2
测试用例
#!/bin/bash
#using numeric test comparisons
var1=10
var2=11
if [ $var1 -gt 5 ]
then
echo "the test value $var1 ia greater than 5"
fi
if [ $var1 -eq $var2 ]
then
echo "the values are equal"
else
echo "the calues are different"
fi
这里注意的是,浮点数不能比较。下面是错误例子
#!/bin/bash
#testing floating point numbers
val1=`echo "scale=4;10/3" | bc`
echo "the test values is $val1"
if [ $val1 -gt 3 ]
then
echo "the result if larger than 3"
fi
这里bash提示需要整数表达式
第二类 字符串的比较
1. 检查str1是否和str2相同:str1 = str2
2. 检查str1是否和str2不同:str1 != str2
3. 检查str1是否比str2小:str1 <str2
4. 检查str1是否比str2大:str1 >str2
5. 检查str1的长度是否为非零:-n str1
6. 检查str1的长度是否为0:-z str1
下面是实例代码
#!/bin/bash
#testing string equality
testuser=dreamlife
if [ $USER = $testuser ]
then
echo "Welcom $testuser"
fi
#!/bin/bash
#testing string equality
testuser=dreamlife
if [ $USER != $testuser ]
then
echo "this user is not $testuser"
else
echo "Welcom $testuser"
fi
在字符串比较的时候,需要注意两个问题,
1. 大于小于符号必须使用转义,否则shell会把他们当做重定向符号而把字符串当做文件名
2. 大于小于顺序和sort命令所采用不同
第一个问题
#!/bin/bash
#mis-using string comparisons
val1=baseball
val2=hockey
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
第二个儿问题
#!/bin/bash
#testing string sort order
val1=testing
val2=Testing
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
字符串大小
#!/bin/bash
#testing string length
val1=testing
val2=''
if [ -n "$val1" ]
then
echo "the string '$val1' is not empty"
else
echo "the string '$val1' is empty"
fi
if [ -z "$val2" ]
then
echo "the string '$val2' is empty"
else
echo "the string '$val2' is not empty"
fi
后面还有文件的比较,由于文件比较内容比较多,我会在写一个。
相关文章推荐
- Linux编程 23 shell编程(结构化条件判断 命令if -then , if-then ... elif-then ...else,if test)
- Linux-Shell脚本编程-学习-6-Shell编程-使用结构化命令-文件比较-case编程
- shell脚本编程-使用结构化命令(if/else)(转)
- Shell脚本语法--if/then/elif/else/fi
- Linux-Shell脚本编程-学习-2-Linux基本命令
- shell脚本编程基础(二)----使用结构化命令
- Shell脚本编程基础 三 使用结构化命令
- Shell脚本语法-- if/then/elif/else/fi
- Shell脚本语法-- if/then/elif/else/fi
- Linux命令与shell脚本编程学习笔记-16创建函数
- Linux-Shell脚本编程-学习-4-Shell编程-操作数字-加减乘除计算
- Shell脚本语法-- if/then/elif/else/fi
- Shell脚本语法-- if/then/elif/else/fi
- linux之shell编程if-then-else结构
- shell结构化命令if-then-elif-fi
- shell脚本:使用if-then语句和test命令
- Shell脚本学习与Linux下vim命令使用
- Linux命令shell脚本编程学习笔记-18章初识sed和gawk
- Linux-Shell脚本编程-学习-1-Linux基本命令
- Linux-Shell脚本编程-学习-3-Shell编程-shell脚本基本格式