linux shell if语句使用方法 [转载]
if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fiFILE 参数是 /dev/fd/N 这样的形式,那么就检查文件描述符 “N”。stdin, stdout 和 stderr 和他们各自的文件描述符也可以用于测试。 7.1.1.1. 和if使用的表达式
| Primary | 意义 |
|---|---|
[ -a FILE ] |
如果 FILE 存在则为真。 |
[ -b FILE ] |
如果 FILE 存在且是一个块特殊文件则为真。 |
[ -c FILE ] |
如果 FILE 存在且是一个字特殊文件则为真。 |
[ -d FILE ] |
如果 FILE 存在且是一个目录则为真。 |
[ -e FILE ] |
如果 FILE 存在则为真。 |
[ -f FILE ] |
如果 FILE 存在且是一个普通文件则为真。 |
[ -g FILE ] |
如果 FILE 存在且已经设置了SGID则为真。 |
[ -h FILE ] |
如果 FILE 存在且是一个符号连接则为真。 |
[ -k FILE ] |
如果 FILE 存在且已经设置了粘制位则为真。 |
[ -p FILE ] |
如果 FILE 存在且是一个名字管道(F如果O)则为真。 |
[ -r FILE ] |
如果 FILE 存在且是可读的则为真。 |
[ -s FILE ] |
如果 FILE 存在且大小不为0则为真。 |
[ -t FD ] |
如果文件描述符 FD 打开且指向一个终端则为真。 |
[ -u FILE ] |
如果 FILE 存在且设置了SUID (set user ID)则为真。 |
[ -w FILE ] |
如果 FILE 如果 FILE 存在且是可写的则为真。 |
[ -x FILE ] |
如果 FILE 存在且是可执行的则为真。 |
[ -O FILE ] |
如果 FILE 存在且属有效用户ID则为真。 |
[ -G FILE ] |
如果 FILE 存在且属有效用户组则为真。 |
[ -L FILE ] |
如果 FILE 存在且是一个符号连接则为真。 |
[ -N FILE ] |
如果 FILE 存在 and has been mod如果ied since it was last read则为真。 |
[ -S FILE ] |
如果 FILE 存在且是一个套接字则为真。 |
[ FILE1 -nt FILE2 ] |
如果 FILE1 has been changed more recently than FILE2, or 如果 FILE1FILE2 does not则为真。 exists and |
[ FILE1 -ot FILE2 ] |
如果 FILE1 比 FILE2 要老, 或者 FILE2 存在且 FILE1 不存在则为真。 |
[ FILE1 -ef FILE2 ] |
如果 FILE1 和 FILE2 指向相同的设备和节点号则为真。 |
[ -o OPTIONNAME ] |
如果 shell选项 “OPTIONNAME” 开启则为真。 |
[ -z STRING ] |
“STRING” 的长度为零则为真。 |
[ -n STRING ] or [ STRING ] |
“STRING” 的长度为非零 non-zero则为真。 |
| [ STRING1 == STRING2 ] | 如果2个字符串相同。 “=” may be used instead of “==” for strict POSIX compliance则为真。 |
| [ STRING1 != STRING2 ] | 如果字符串不相等则为真。 |
| [ STRING1 < STRING2 ] | 如果 “STRING1” sorts before “STRING2” lexicographically in the current locale则为真。 |
| [ STRING1 > STRING2 ] | 如果 “STRING1” sorts after “STRING2” lexicographically in the current locale则为真。 |
| [ ARG1 OP ARG2 ] | “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers. |
| 操作 | 效果 |
|---|---|
| [ ! EXPR ] | 如果 EXPR 是false则为真。 |
| [ ( EXPR ) ] | 返回 EXPR的值。这样可以用来忽略正常的操作符优先级。 |
| [ EXPR1 -a EXPR2 ] | 如果 EXPR1 and EXPR2 全真则为真。 |
| [ EXPR1 -o EXPR2 ] | 如果 EXPR1 或者 EXPR2 为真则为真。 |
7.1.1.2. 后接then语句的命令
7.1.1.3. 检查文件
anny ~>catmsgcheck.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.
7.1.1.4. 检查shell选项
# 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
7.1.2.2. 数字的比较
anny >num=`wc -l work.txt`anny >echo$num201anny >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 >
#!/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
7.1.2.3. 字符串比较
if [ "$(whoami)" != 'root' ]; then echo "You have no permission to run $0 as non-root user." exit 1; fi
[ "$(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.
shell if 命令参数说明-b 当file存在并且是块文件时返回真
-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 非空串
linux shell if语句使用方法 [转载]的更多相关文章
- linux shell if语句使用方法
原文地址:http://blog.chinaunix.net/uid-24607609-id-2118151.html 最精简的 if 命令的语法是: if TEST-COMMANDS; then C ...
- linux shell 中数组使用方法介绍
linux shell在编程方面比windows 批处理强大太多,不管是在循环.运算.已经数据类型方面都是不能比較的. 以下是个人在使用时候,对它在数组方面一些操作进行的总结. 1.数组定义 [che ...
- linux修改用户主目录的方法 (转载)
转自:http://xiaomaimai.blog.51cto.com/1182965/274002 第一:修改/etc/passwd文件第二:usermod命令 详细说明如下:第一种方法:vi /e ...
- 【转】linux shell实现随机数多种方法(date,random,uuid)
在日常生活中,随机数实际上经常遇到,想丢骰子,抓阄,还有抽签.呵呵,非常简单就可以实现.那么在做程序设计,真的要通过自己程序设计出随机数那还真的不简单了.现在很多都是操作系统内核会提供相应的api,这 ...
- linux shell编程语句if、case.
shell学习笔记--if,case shell的控制流结构主要有if语句.for语句.case语句.while语句.until语句这五种,在shell中这些语句的用法有点类似C语言,很容易学会,但也 ...
- linux kernel 结构体赋值方法{转载}
原文地址: http://www.chineselinuxuniversity.net/articles/48226.shtml 这几天看Linux的内核源码,突然看到init_pid_ns这个结构体 ...
- linux shell实现随机数多种方法(date,random,uuid)
参考: http://www.cnblogs.com/chengmo/archive/2010/10/23/1858879.html $ cat /proc/sys/kernel/random/uui ...
- linux shell if语句
#!/bin/bash read -p "please input Y/N" keyWord if [ "$keyWord" == "Y" ...
- Linux shell 日期,时间相关的命令
在shell脚本中,经常要用到跟获取日期相关的东西,这里记录一下Linux shell 获取日期的方法 获取当前日期:today=`date +"%Y-%m-%d"` 获取昨天的日 ...
随机推荐
- CF989C A Mist of Florescence
思路: 有趣的构造题. 实现: #include <bits/stdc++.h> using namespace std; ][]; void fillin(int x, int y, c ...
- 获取页面URL两种方式
以请求http://localhost:8080/doctor/demo?code=1为例 一:用java代码获取 //获取URL中的请求参数.即?后的条件 code=1 String querySt ...
- 初探ABP--记一些常见的开发问题
1.Update-Database : 无法将“Update-Database”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次. ...
- jquery命名冲突
nodeName是jquery的关键字
- 富通天下(T 面试)
1.Mybatis的分页查询是怎么实现的? 交流: A:我们是通过PageHelper插件实现的 B:你说下原生SQL应该怎么写? A:某段查询SQL,实现分页需要使用limit关键字,改变下标和页码 ...
- Modal 高度 在里面css里写高 | iview
.modalCss { height: 330px; overflow: auto; padding-right: 10px; }
- Open Cascade:使用鼠标画线
Open Cascade:使用鼠标画线 在View类文件中创建以下代码: 1.创建鼠标消息: afx_msg void OnLButtonDown(UINT nFlags, CPoint point) ...
- Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1146
问题介绍: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1146 MySql语法错误, 但是错 ...
- css--字体和文本样式
文字样式属性 字体:font-family 文字大小:font-size 文字颜色:font-color 文字粗细:font-weight 文字样式:font-style font-family字体属 ...
- select onchange事件的使用
<select name="expireDay" id="expireDay" class="form-control" onchan ...