linux shell中的条件判断语句
http://bbs.chinaunix.net/thread-396805-1-1.html
shell 判断语句
流程控制 "if" 表达式 如果条件为真则执行then后面的部分:
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)指定的设备为终端时为真
-n variable 判断一个变量是否有值
-z variable 判断一个变量是否为非空字符串
#########################################################
含条件选择的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命令选项在执行某一文件之前检查它是否存在。 ################################################################# # 判断文件是否存在 #!/bin/sh today=`date -d yesterday +%y%m%d` file="apache_$today.tar.gz" cd /home/chenshuo/shell if [ -f "$file" ];then echo "OK" else echo "error $file" >error.log mail -s "fail backup from test" chenshuo@soufun.com <error.log fi =================================================
1.shell判断文件,目录是否存在或者具有权限 2. #!/bin/sh 3. 4.myPath= "/var/log/httpd/" 5.myFile= "/var /log/httpd/access.log" 6. 7.#这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 8.if [ ! -x "$myPath" ]; then 9.mkdir "$myPath" 10.fi 11. 12.#这里的-d 参数判断$myPath是否存在 13.if [ ! -d "$myPath" ]; then 14.mkdir "$myPath" 15.fi 16. 17.#这里的-f参数判断$myFile是否存在 18.if [ ! -f "$myFile" ]; then 19.touch "$myFile" 20.fi 21. 22.#其他参数还有-n,-n是判断一个变量是否有值 23.if [ ! -n "$myVar" ]; then 24.echo "$myVar is empty" 25.exit 0 26.fi 27. 28.#两个变量判断是否相等 29.if [ "$var1" = "$var2" ]; then 30.echo '$var1 eq $var2' 31.else 32.echo '$var1 not eq $var2' 33.fi
/****************************************************************************/
1 判断一个变量是否被定义
if [ -z $EDITOR ]
2 判断交互模式
if [ -t ]
3 测试文件权限
if [ ! -w "$LOGFILE"]
4 测试SHELL命令
if echo $list | grep "Peter" > /dev/null 2>&1
5 测试数值
if [ "10" -lt "12" ]
* -b file = True if the file exists and is block special file. 如果该文件存在并且是块特殊文件。
* -c file = True if the file exists and is character special file.如果该文件存在并且是字符特殊文件
* -d file = True if the file exists and is a directory. 如果该文件存在并且是一个目录。
* -e file = True if the file exists. 如果该文件存在
* -f file = True if the file exists and is a regular file 如果该文件存在并且是一个普通文件
* -g file = True if the file exists and the set-group-id bit is set. 如果该文件存在并且设置了组ID位。
* -k file = True if the files’ “sticky” bit is set. 如果文件的sticky “粘性”位被设置。
* -L file = True if the file exists and is a symbolic link. 该文件存在并且是一个符号链接。
* -p file = True if the file exists and is a named pipe. 该文件存在并且是一个命名管道。
* -r file = True if the file exists and is readable. 文件存在并且是可读的
* -s file = True if the file exists and its size is greater than zero. 文件存在,它的大小是大于零
* -S file = True if the file exists and is a socket. 文件存在并且是一个套接字
* -t fd = True if the file descriptor is opened on a terminal. 文件描述符是在一个终端上打开的
* -u file = True if the file exists and its set-user-id bit is set. 文件存在,它的设置用户ID位被设置了
* -w file = True if the file exists and is writable. 文件存在并且可写
* -x file = True if the file exists and is executable. 文件存在并且是可执行的
* -O file = True if the file exists and is owned by the effective user id. 文件存在并且是所拥有的有效用户ID
* -G file = True if the file exists and is owned by the effective group id. 文件存在并且拥有有效的gruop id。
* file1 -nt file2 = True if file1 is newer, by modification date, than file2. 如果file1更新
* file1 ot file2 = True if file1 is older than file2. 如果file1更旧
* file1 ef file2 = True if file1 and file2 have the same device and inode numbers.file1和file2有相同的设备和节点号
* -z string = True if the length of the string is 0. 字符串的长度为0
* -n string = True if the length of the string is non-zero. 字符串的长度不为零
* string1 = string2 = True if the strings are equal.
*string1 != string2 = True if the strings are not equal.
!expr = True if the expr evaluates to false.
* expr1 -a expr2 = True if both expr1 and expr2 are true. 且为真
* expr1 -o expr2 = True is either expr1 or expr2 is true. 或
两个档案之间的判断与比较 ;例如『 test file1 -nt file2 』 * -nt 第一个档案比第二个档案新 * -ot 第一个档案比第二个档案旧 * -ef 第一个档案与第二个档案为同一个档案( link 之类的档案)
逻辑的『和(and)』『或(or)』 * && 逻辑的 AND 的意思 * || 逻辑的 OR 的意思
运算符号 代表意义 = 等于 != 不等于 < 小于 > 大于 -eq 等于 -ne 不等于 -lt 小于 -gt 大于 -le 小于或等于 -ge 大于或等于 -a 双方都成立(and) -o 单方成立(or) -z 空字符串 -n 非空字符串
/************************************************************/
格式如下,在比较时,数字和字符串用不同的比较符号
1.如果a>b且a<c
if (( a > b )) && (( a < c )) 或者
if [[ $a > $b ]] && [[ $a < $c ]]
或者 if [ $a -gt $b -a $a -lt $c ]
2.如果a>b或a<c
if (( a > b )) || (( a < c )) 或者 if [[ $a > $b ]] || [[ $a < $c ]]
或者 if [ $a -gt $b -o $a -lt $c ]
3. -o = or , -a = and , 但我一向只用 || 或者 &&
4."||"和"&&"在SHELL里可以用吗?也就是第一个写成if [ a>b && a<c ]也可以吗?
可用, 但是要两个独立的 [ ] , [[ ]] 或 (( )) 看 1
5 -ne 比较数字 (numberic) ; != 比较字符 (string), 但后者拿来 比较数字也可,只是不是标准用法 -lt 是等同 < , 但 < 只能在 shell 的数值操作符 (( )) 或 者 逻缉操作符 [[ ]] 才可使用, -lt , -eq , -gt , -ge -le , 这些是 test , 就是 [ ] 这个内建命令使用的条件操 作符, 数字使用; = , != 字符用, == 这个该是 [[ ]] 用的, 可用来比对正规表示式, 但用在 [ ] 也可,只是不太正统用法 /************************************************************/
test命令用法。功能:检查文件和比较值
1)判断表达式 if test (表达式为真) if test !表达式为假
-----
linux shell中的条件判断语句的更多相关文章
- shell中的条件判断以及与python中的对比
shell中比如比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示条件测试. 注意:这里的空格很重要.要确保方括号的空格. if ....; then ...
- shell编程2 ---条件判断语句
shell编程2 一.条件判断语句 1.按照文件类型进行判断 eg:test -e /etc/passwd 或者 [ -e /etc/passwd ] &&echo yes || ...
- shell中的条件判断if和测试
(一)条件判断 if 中-z 到 -d 的意思 [ -a file ] 若file存在,则为真. [ -b file ] 若file存在且是一个块特殊文件,则为真. [ -c file ] 若file ...
- javascript中对条件判断语句的优化 分类: JavaScript 2015-06-07 09:54 832人阅读 评论(2) 收藏
不管写什么程序,平时都会用到条件语句,如:if...else... switch这样的语句,来达到对条件的判断.下面看来一段代码: function abc(test){ if (test == 1) ...
- shell中的条件判断、参数以及变量替换
文章转自: http://www.cnblogs.com/maxupeng/archive/2011/07/02/2096551.html 一.test命令 test命令是shell内部命令,它计算作 ...
- SQL中的条件判断语句(case when zhen if,ifnull)用法
简介: case具有两种格式.简单case函数和case搜索函数.这两种方式,可以实现相同的功能.简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式.还有 ...
- shell中的条件判断
read命令 命令的语法: read [参数] 变量名 常用的参数如下: -t timeout 设定超时时间. -p prompt 设定提示信息,该提示信息将会显在光标前. 条件测试: 条件测试可以判 ...
- Shell编程中的条件判断(条件测试)
Shell中的条件判断(测试)类型: 1) 整数测试 2) 字符测试 3) 文件测试 条件测试的表达式: (注: expression 与 [] 之间空格不能省略) [ expressi ...
- linux Shell中常用的条件判断
linux Shell中常用的条件判断 -b file 若文件存在且是一个块特殊文件,则为真 -c file 若文件存在且是一个字符特殊文件,则为真 -d ...
随机推荐
- 痞子衡嵌入式:聊聊i.MXRT1xxx上的普通GPIO与高速GPIO差异及其用法
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT上的普通GPIO与高速GPIO差异. GPIO 可以说是 MCU 上最简单最常用的外设模块了,当一些原生功能外设接口模块不能 ...
- 修改记事本PE结构弹计算器Shellcode
目录 修改记事本PE结构弹计算器Shellcode 0x00 前言 0x01 添加新节 修改节数量 节表位置 添加新节表信息 0x02 添加弹计算器Shellcode 修改代码 0x03 修改入口点 ...
- typedef的用法 单向链表的查找、增加、删除、销毁。
一:typedef的用法. 写一个数据结构(计算机存储数据的一种方式,是抽象的,可以人为组织,提高算法效率),我们需要注意:接口友好,模块化,规范命名等方面,在接口友好方面,typedef是非常 ...
- c++ template 实现一个简单的"栈"
一: 实现一个简单的swap 原来我们写swap一定会这样写: 对于int类型的: swap(const int &x,const int &y) { int temp; temp = ...
- 装了这几个IDEA插件,基本上一站式开发了!
前言 前几天有社区小伙伴私聊我,问我都用哪些IDEA插件,我的IDEA的主题看起来不错. 作为一个开源作者,每周要code大量的代码,提升日常工作效率是我一直追求的,在众多的IDEA插件中,我独钟爱这 ...
- prometheus(2)之对kubernetes的监控
prometheus服务发现 1.基于endpoints的service注释服务自动发现. 2.基于pod注释的服务自动发现 3.基于consul注册的服务自动发现 4.手动配置服务发现 5.push ...
- Spring Ioc 容器初始化过程
IOC 是如何工作的? 通过 ApplicationContext 创建 Spring 容器,容器读取配置文件 "/beans.xml" 并管理定义的 Bean 实例对象. 通 ...
- 史上最全的Excel导入导出之easyexcel
喝水不忘挖井人,感谢阿里巴巴项目组提供了easyexcel工具类,github地址:https://github.com/alibaba/easyexcel 文章目录 环境搭建 读取excel文件 小 ...
- [bzoj1122]账本
简化问题:如果没有2操作,答案是多少贪心:修改-一定修改最前面的,修改+一定修改最后面的,正确性显然而通过1操作,要完成两步:1.让最终结果为q:2.让前缀和非负,通过贪心可以获得最小值(具体来说,假 ...
- 痞子衡嵌入式:深扒IAR启动函数流程之段初始化函数__iar_data_init3实现
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是IAR启动函数流程里的段初始化函数__iar_data_init3实现. 本篇是 <IAR启动函数流程及其__low_level_ ...