Linux命令之if - Bash中的条件判断语句
2015-01-04 19:45
519 查看
用途说明
Shell中的条件判断语句,与其他编程语言类似。如果需要知道有哪些条件判断方式,通过man test就可以得到帮助。常用格式
格式一
if 条件; then 语句fi格式二
if 条件; then 语句else 语句fi格式三
if 条件; then 语句elif 条件; then 语句fi格式四
if 条件; then 语句elif 条件; then 语句else 语句fi使用示例
示例一
Bash代码if [ "foo" = "foo" ]; then
echo expression evaluated as true
fi
[root@jfht ~]# if [ "foo" = "foo" ]; then
> echo expression evaluated as true
> fi
expression evaluated as true
[root@jfht ~]#
示例二
Bash代码if [ "foo" = "foo" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
[root@jfht ~]# if [ "foo" = "foo" ]; then
> echo expression evaluated as true
> else
> echo expression evaluated as false
> fi
expression evaluated as true
[root@jfht ~]#
示例三
Bash代码T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
[root@jfht ~]# T1="foo"
[root@jfht ~]# T2="bar"
[root@jfht ~]# if [ "$T1" = "$T2" ]; then
> echo expression evaluated as true
> else
> echo expression evaluated as false
> fi
expression evaluated as false
[root@jfht ~]#
示例四 判断命令行参数数量
文件 if_4.shBash代码#!/bin/sh
if [ "$#" != "1" ]; then
echo "usage: $0 <file>"
exit 1
fi
[root@smsgw root]# cat if_4.sh
#!/bin/sh
if [ "$#" != "1" ]; then
echo "usage: $0 <file>"
exit 1
fi
[root@smsgw root]# chmod +x if_4.sh
[root@smsgw root]# ./if_4.sh
usage: ./if_4.sh <file>
[root@smsgw root]# ./if_4.sh hello
[root@smsgw root]#
示例五 判断文件中是否包含某个字符串
Bash代码if grep -q root /etc/passwd; then
echo account root exists
else
echo account root not exist
fi
[root@jfht ~]# if grep -q root /etc/passwd; then
> echo account root exists
> else
> echo account root not exist
> fi
account root exists
[root@jfht ~]#
示例六 判断文件是否存在
Bash代码if [ -e myfile ]; then
echo myfile exists
else
touch myfile
echo myfile created
fi
[root@jfht ~]# if [ -e myfile ]; then
> echo myfile exists
> else
> touch myfile
> echo myfile created
> fi
myfile created
[root@jfht ~]# if [ -e myfile ]; then
> echo myfile exists
> else
> touch myfile
> echo myfile created
> fi
myfile exists
[root@jfht ~]# ls -l myfile
-rw-r--r-- 1 root root 0 10-09 20:44 myfile
示例七 判断两个文件是否相同
Bash代码echo 1 >file1
echo 2 >file2
if ! diff -q file1 file2; then
echo file1 file2 diff
else
echo file1 file2 same
fi
[root@jfht ~]# echo 1 >file1
[root@jfht ~]# echo 2 >file2
[root@jfht ~]# if ! diff -q file1 file2; then
> echo file1 file2 diff
> else
> echo file1 file2 same
> fi
Files file1 and file2 differ
file1 file2 diff
[root@jfht ~]#
问题思考
1. 怎么判断字符串非空?2. 怎么判断文件非空?3. 怎么判断文件可执行?4. 怎么判断目录?5. 怎么判断数值大小判断?相关资料
【1】BASH Programming 6.1 Dry Theory【2】刘 泰山的博客 bash if 条件判断比如比较字符串、判断文件是否存在及是否可读等,通常用"[]"来表示条件测试。
注意:这里的空格很重要。要确保方括号的空格。笔者就曾因为空格缺少或位置不对,而浪费好多宝贵的时间。
if ....; then
....
elif ....; then
....
else
....
fi
[ -f "somefile" ] :判断是否是一个文件
[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可执行权限
[ -n "$var" ] :判断$var变量是否有值
[ "$a" = "$b" ] :判断$a和$b是否相等
-r file 用户可读为真
-w file 用户可写为真
-x file 用户可执行为真
-f file 文件为正规文件为真
-d file 文件为目录为真
-c file 文件为字符特殊文件为真
-b file 文件为块特殊文件为真
-s file 文件大小非0时为真
-t file 当文件描述符(默认为1)指定的设备为终端时为真含条件选择的shell脚本 对于不含变量的任务简单shell脚本一般能胜任。但在执行一些决策任务时,就需要包含if/then的条件判断了。shell脚本编程支持此类运算,包括比较运算、判断文件是否存在等。
基本的if条件命令选项有: - eq —比较两个参数是否相等(例如,if [ 2 –eq 5 ])
-ne —比较两个参数是否不相等
-lt —参数1是否小于参数2
-le —参数1是否小于等于参数2
-gt —参数1是否大于参数2
-ge —参数1是否大于等于参数2
-f — 检查某文件是否存在(例如,if [ -f "filename" ])
-d — 检查目录是否存在
几乎所有的判断都可以用这些比较运算符实现。脚本中常用-f命令选项在执行某一文件之前检查它是否存在
相关文章推荐
- shell基础之二:条件判断语句if、case及read命令,
- bash编程之条件判断,if语句!
- linux if 命令判断条件总结
- Linux编程 23 shell编程(结构化条件判断 命令if -then , if-then ... elif-then ...else,if test)
- linux grep egrep fgrep bash条件判断 bash测试 if 条件判断
- Linux学习命令汇总四——Bash中变量.文件通配符.管道与重定向.算术运算及循环或判断语句
- bash条件判断之if语句
- linux if 命令判断条件总结
- linux if 命令判断条件总结
- bash条件判断之if语句(二)
- 条件判断之if、case语句和文件查找命令
- Linux基础入门及系统管理01-bash脚本编程之二if等条件判断脚本知识17
- 多条件if()判断语句
- 【面试题】不用if,条件表达式,switch等判断语句------找出两个数中较大的
- linux if语句内判断参数
- Linux--Shell 编程-bash,命令替换,if分支嵌套,运算,输入输出
- linux shell if else 多个选择条件的判断语句 语法
- linux if判断命令
- 求1+2+3+...+n,要求不能使用乘除法,for,while,if,else,switch,case等关键字以及条件判断语句
- 【练习】题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case 等关键字以及条件判断语句