shell的if判断
来源:互联网 发布:unity3d沙盘性能 编辑:程序博客网 时间:2024/05/18 01:49
1 概要
有时候你需要指定shell脚本中的依靠命令的成功与否来实施不同过程的行为。 if 结构允许你来指定这样的条件。
最精简的 if 命令的语法是:
if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi
TEST-COMMAND 执行后且它的返回状态是0,那么 CONSEQUENT-COMMANDS 就执行。返回状态是最后一个命令的退出状态,或者当没有条件是真的话为0。
TEST-COMMAND 经常包括数字和字符串的比较测试,但是也可以是任何在成功时返回状态0或者失败时返回一些其他状态的一些命令。一元表达式经常用于检查文件的状态。如果对某个要素primaries, FILE 参数是 /dev/fd/N 这样的形式,那么就检查文件描述符 “N”。stdin, stdout 和 stderr 和他们各自的文件描述符也可以用于测试。
1.1 和if使用的表达式
下表包含了一个组成 TEST-COMMAND 命令或者命令列表,称作 “要素primaries” 的概览。这些primaries放置在方括号中来表示一个条件表达式的测试。
表 1.1. 主表达式
表达式可以借以下操作符组合起来,以降序列出:listed in decreasing order of precedence:
表 1.2. 组合表达式
[ (或作 test) 内建命令对条件表达式使用一系列基于参数数量的规则来求值。更多关于这个主题的信息可以在Bash文档中查找。就像if 使用fi 来结束一样,在条件列完之后必须用">"来结束。
1.2. 后接then语句的命令
CONSEQUENT-COMMANDS 列出了跟在 then 语句后面可以是任何有效的UNIX命令,任何可执行的程序,任何可执行的shell脚本或者任何shell语句,除了 fi. 。重要地记住then 和 fi 在shell里面被认为是分开的语句。因此,在命令行上使用的时候,他们用分号隔开。
在脚本中,if语句的不同部分通常是良好分隔的。以下是一些简单的例子:
1.3. 检查文件
第一个例子检查一个文件是否存在:
anny ~> cat msgcheck.sh#!/bin/bash
echo "This scripts checks the existence of the messages file."
echo "Checking..."
if [ -f /var/log/messages ]
then
echo "/var/log/messages exists."
fi
echo
echo "...done."
anny ~> ./msgcheck.sh
This scripts checks the existence of the messages file.
Checking...
/var/log/messages exists.
...done.
1.4. 检查shell选项
加入到你的Bash配置文件中去:
# These lines will print a message if the noclobber option is set:if [ -o noclobber ]
then
echo "Your files are protected against accidental overwriting using redirection."
fi
以上的例子将在命令行输入后开始工作:
anny ~> if [ -o noclobber ] ; then echo ; echo"your files are protectedagainst overwriting." ; echo ; fi
your files are protected against overwriting.
anny ~>
然而,如果你使用依赖环境的测试,当你在脚本中输入相同的命令你可能得到不用的结果,因为脚本会打开一个新的,没有设置预期的变量和选项的shell。
1.2. if的简单应用
1.2.1. 测试退出状态
? 变量包含了之前执行命令的退出状态(最近完成的前台进程)。
以下的例子显示了一个简单的测试:
anny ~> if [ $? -eq 0 ]More input> then echo 'That was a good job!'
More input> fi
That was a good job!
anny ~>
以下的例子证明了 TEST-COMMANDS 可以是任何有返回和退出状态的UNIX命令,之后if 再次返回零的退出状态:
anny ~> if ! grep $USER /etc/passwdMore input> then echo "your user account is not managed locally"; fi
your user account is not managed locally
anny > echo $?
0
anny >
以下能得到同样的结果:
anny > grep $USER /etc/passwdanny > if [ $? -ne 0 ] ; then echo "not a local account" ; fi
not a local account
anny >
7.1.2.2. 数字的比较
以下的例子是用了数值的比较:
anny > num=`wc -l work.txt`anny > echo $num
201
anny > if [ "$num" -gt "150" ]
More input> then echo ; echo "you've worked hard enough for today."
More input> echo ; fi
you've worked hard enough for today.
anny >
这个脚本在每个星期天由cron来执行。如果星期的数是偶数,他就提醒你把垃圾箱清理:
#!/bin/bash# Calculate the week number using the date command:
WEEKOFFSET=$[ $(date +"%V") % 2 ]
# Test if we have a remainder. If not, this is an even week so send a message.
# Else, do nothing.
if [ $WEEKOFFSET -eq "0" ]; then
echo "Sunday evening, put out the garbage cans." | mail -s "Garbage cans out" your@your_domain.org
1.2.3. 字符串比较
一个通过比较字符串来测试用户ID的例子:
if [ "$(whoami)" != 'root' ]; thenecho "You have no permission to run $0 as non-root user."
exit 1;
fi
使用Bash,你可以缩短这样的结构。下面是以上测试的精简结构:
[ "$(whoami)" != 'root' ] && ( echo you are using a non-privileged account; exit 1 )类似于如果测试为真就执行的 “&&” 表达式, “||” 指定了测试为假就执行。类似于 “&&” 表达式指明了在两个测试条件为真时所采取的动作,“||” 指明测试为假时所采取的行动。
正则表达式也可以在比较中使用:
anny > gender="female"anny > if [[ "$gender" == f* ]]
More input> then echo "Pleasure to meet you, Madame."; fi
Pleasure to meet you, Madame.
anny >
多数程序员更喜欢使用和方括号相同作用的内建的 test 命令,像这样:
test "$(whoami)" != 'root' && (echo you are using a non-privileged account; exit 1)参见信息页面得到更多关于Bash “(( EXPRESSION ))” 和 “[[ EXPRESSION ]]” 结构的模块匹配信息。
shell if 命令参数说明
-c 当file存在并且是字符文件时返回真
-d 当pathname存在并且是一个目录时返回真
-e 当pathname指定的文件或目录存在时返回真
-f 当file存在并且是正规文件时返回真
-g 当由pathname指定的文件或目录存在并且设置了SGID位时返回为真
-h 当file存在并且是符号链接文件时返回真,该选项在一些老系统上无效
-k 当由pathname指定的文件或目录存在并且设置了“粘滞”位时返回真
-p 当file存在并且是命令管道时返回为真
-r 当由pathname指定的文件或目录存在并且可读时返回为真
-s 当file存在文件大小大于0时返回真
-u 当由pathname指定的文件或目录存在并且设置了SUID位时返回真
-w 当由pathname指定的文件或目录存在并且可执行时返回真。一个目录为了它的内容被访问必然是可执行的。
-o 当由pathname指定的文件或目录存在并且被子当前进程的有效用户ID所指定的用户拥有时返回真。
比较字符写法:
-eq 等于
-ne 不等于
-gt 大于
-lt 小于
-le 小于等于
-ge 大于等于
-z 空串
* = 两个字符相等
* != 两个字符不等
* -n 非空串
转自:http://hi.baidu.com/caosicong/blog/item/7873e2166b8f6154f3de3280.html/cmtid/dd0b8924710a0f3c8744f986
----------------------------------------------------------------------------------------------------------------------------------------
OK,接下来自己写一个简单的挂载硬盘脚本,mount_disk.sh(windows下的ntfs格式硬盘)
点击(此处)折叠或打开
- #! /bin/sh
- dir_d=/media/disk_d
- dir_e=/media/disk_e
- dir_f=/media/disk_f
- a=`ls $dir_d| wc -l`
- b=`ls $dir_e| wc -l`
- c=`ls $dir_f| wc -l`
- echo "checking disk_d..."
- if [ $a-eq 0 ]; then
- echo "disk_d is not exsit,now creating..."
- sudo mount -t ntfs /dev/disk/by-label/software/media/disk_d
- else
- echo "disk_d exits"
- fi
- echo "checking disk_e..."
- if [ $b-eq 0 ]; then
- echo "disk_e is not exsit,now creating..."
- sudo mount -t ntfs /dev/disk/by-label/elitor/media/disk_e
- else
- echo "disk_e exits"
- fi
- echo "checking disk_f..."
- if [ $c -eq 0]; then
- echo "disk_f is not exsit,now creating..."
- sudo mount -t ntfs /dev/disk/by-label/work/media/disk_f
- else
- echo "disk_f exits"
- fi
- shell的if判断
- shell之if判断的总结
- shell中if判断的参数
- shell编程之if判断的总结
- shell编程之if判断的总结
- shell编程之if判断的总结
- 【shell】脚本中if条件的判断
- shell编程之if判断的总结
- shell编程之if判断的总结
- shell编程之if判断的总结
- shell脚本下if判断的使用
- shell if 判断语句
- Shell IF判断语句
- shell if判断
- shell中的if判断
- shell中的if判断
- [shell] if判断条件
- Shell中if判断
- Android如何通过无线网络连接adb
- 什么是GOP
- 一看肠一断,好去莫回头
- Additive equations
- Android 9.png 特点说明
- shell的if判断
- VC++ 生成Release
- MyEclipse6.5 Alt+/
- OpenCV Python教程(3、直方图的计算与显示)
- 正则表达式 linux shell
- 怎样有效Shrink(压缩)Guest系统为Linx/Unix的VMWare虚拟机VMDK文件
- What's new in windows server 2012 failover clustering (Part 1)
- 浅析java中哪些方法不能被覆写
- Hadoop源码分析笔记(十):数据节点--流式接口的实现