当前位置: > shell编程 >

shell if条件表达式(shell if条件判断语句)用法教程

时间:2015-02-27 01:16来源:linux.it.net.cn 作者:IT
有关shell if条件判断语句的一些例子,shell if条件表达式用法,shell if else语句的实例教程。


bash shell if语句用法

if your_command; then.. 会现执行your_command,如果返回值为0,则会执行then后的语句;
否则会执行后面的elif或者else后的语句。
 

复制代码 代码示例:if g++ -v > /dev/null 2>&1; then
echo "OK"
else
echo "failed. Install the \"GNU C++ compiler\"!"
exit 1
fi

#the above and the below have the same function
g++ -v > /dev/null 2>&1
if [ $? = 0 ]; then
echo "OK"
else
echo "failed. Install the \"GNU C++ compiler\"!"
exit 1
fi

shell if else语句

和Java、PHP等语言不一样,sh的流程控制不可为空,例如:
 

复制代码 代码示例:<?php
if (isset($_GET["q"])) {
search(q);
}
else {
//do nothing
}
?>
在sh/bash里可不能这么写,如果else分支没有语句执行(shell条件判断语句),就不要写这个else,就像这样:
 

复制代码 代码示例:if condition
then
command1
command2
...
commandN
fi
当然,也可以写成一行(适用于终端命令提示符),像这样:
if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi
末尾的fi就是if倒过来拼写,后面还会遇到类似的。

if else格式
if condition
then
command1
command2
...
commandN
else
command
fi

if else-if else格式
if condition1
then
command1
elif condition2
command2
else
commandN
fi


if else语句经常与test命令结合使用,如下所示:
 

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
 
输出:
The two numbers are equal!

Shell case语句
Shell case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。

case语句格式如下:
 

case 值 in
模式1)
command1
command2
...
commandN
;;
模式2)
command1
command2
...
commandN
;;
esac
 
case工作方式如上所示。取值后面必须为单词in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。

取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。

以下脚本提示输入1到4,与每一种模式进行匹配:
 

echo 'Input a number between 1 to 4'
echo 'Your number is:\c'
read aNum
case $aNum in
1)  echo 'You select 1'
;;
2)  echo 'You select 2'
;;
3)  echo 'You select 3'
;;
4)  echo 'You select 4'
;;
*)  echo 'You do not select a number between 1 to 4'
;;
esac
输入不同的内容,会有不同的结果,例如:
 

Input a number between 1 to 4
Your number is:3
You select 3
shell test命令

Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
数值测试
 

参数  说明
-eq  等于则为真
-ne  不等于则为真
-gt  大于则为真
-ge  大于等于则为真
-lt  小于则为真
-le  小于等于则为真
 
例如:
 

num1=100
num2=100
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
输出:
The two numbers are equal!

字符串测试
参数  说明
=  等于则为真
!=  不相等则为真
-z 字符串  字符串长度伪则为真
-n 字符串  字符串长度不伪则为真

例如:
 

num1=100
num2=100
if test num1=num2
then
echo 'The two strings are equal!'
else
echo 'The two strings are not equal!'
fi
 
输出:
The two strings are equal!

文件测试
 

参数  说明
-e 文件名  如果文件存在则为真
-r 文件名  如果文件存在且可读则为真
-w 文件名  如果文件存在且可写则为真
-x 文件名  如果文件存在且可执行则为真
-s 文件名  如果文件存在且至少有一个字符则为真
-d 文件名  如果文件存在且为目录则为真
-f 文件名  如果文件存在且为普通文件则为真
-c 文件名  如果文件存在且为字符型特殊文件则为真
-b 文件名  如果文件存在且为块特殊文件则为真
例如:
 

cd /bin
if test -e ./bash
then
echo 'The file already exists!'
else
echo 'The file does not exists!'
fi
 
输出:
The file already exists!

另外,Shell还提供了与( ! )、或( -o )、非( -a )三个逻辑操作符用于将测试条件连接起来,其优先级为:“!”最高,“-a”次之,“-o”最低。例如:
 

cd /bin
if test -e ./notFile -o ./bash
then
echo 'One file exists at least!'
else
echo 'Both dose not exists!'
fi
输出:
One file exists at least!


 

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容