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"` 获取昨天的日 ...
随机推荐
- 洛谷P2765 魔术球问题(贪心 最大流)
题意 已经很简洁了吧. 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全 ...
- Ant题解
Description: 一根长度为L厘米的木棒上有N只蚂蚁,每只蚂蚁要么向左走,要么向右走,速度为1厘米/秒.当两只蚂蚁相撞时,他们会同时掉头(掉头时间不计)给出每只蚂蚁距离木棒左端的距离,问多少秒 ...
- Android系统级技巧合集
Android系统级技巧合集(随时更新) #转载请注明来源# 1.高通骁龙系列查看CPU体质等级 CPU体质,即为CPU在工作频率下的电压.同一批次的CPU体质各有不同,体质越高,代表该颗CPU可在更 ...
- liunx防火墙的基本操作
centos7中的使用 CentOS 7.0默认使用的是firewall作为防火墙 查看防火墙状态------->firewall-cmd --state 停止firewall-------&g ...
- uva1615 Highway
画图,每个给出点都有对应区间:先sort,再尽量靠右选:很常见的套路了..//注意不要越界(0,L) struct Q //复习结构{ double l,r; Q(double _l,double _ ...
- shift Alt + up(down) copy current line ! ctrl + j show the control # vscode key
shift Alt + up(down) copy current line ! ctrl + j show the control # vscode key
- centOS7安装 mysql-community-release-el7-5.noarch.rpm 包
一.rpm包 1.wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm(下载rpm) 2.rpm -ivh mysql ...
- Qt _六天的学习路线
六天的学习路线:第一天: 1.Qt的介绍 2.Qt的框架 3.项目文件(.pro) 4.第一个Qt程序(hello Qt) 5.父窗口和子窗口的区别(控件,部件,构件) ...
- python3写冒泡排序
1.概念理解: 冒泡排序:可以简单的理解为是列表中相近的元素,两两比较,小的在前面.最多需要len()-1次排序. 2.例子:a=[11,7,4,56,35,0] 3.代码实现: 4.输出结果: 第1 ...
- Web前端技术体系大全搜索
一.前端技术框架 1.Vue.js 官网:https://cn.vuejs.org/ Vue CLI:https://cli.vuejs.org/ 菜鸟教程:http://www.runoob.com ...