shell脚本if语句的多种条件参数
if语句有多种写法
[root@shell-yankerp sh]# [ -f file ] && echo "yes" || echo "no"
yes
修改如下:
if [ -f file ]
then
echo "yes"
else
echo "no"
fi
简写:
[ -f file ] && echo "yes"
表示if条件成立时返回yes
[ -f file ] || echo "no"
表示if条件不成立时,返回no
可以用if判断如下条件:
实际上if的条件就是test命令的参数
[omcr@lnltedmr3 ~]$ man test
TEST(1) User Commands TEST(1)
NAME
test - check file types and compare values
SYNOPSIS
test EXPRESSION
test
[ EXPRESSION ]
[ ]
[ OPTION
DESCRIPTION
Exit with the status determined by EXPRESSION.
--help display this help and exit
--version
output version information and exit
An omitted EXPRESSION defaults to false. Otherwise, EXPRESSION is true or false and sets exit status. It is one of:
( EXPRESSION )
EXPRESSION is true
! EXPRESSION
EXPRESSION is false
EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2
either EXPRESSION1 or EXPRESSION2 is true
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING
the length of STRING is zero
STRING1 = STRING2
the strings are equal
STRING1 != STRING2
the strings are not equal
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2
FILE1 is older than FILE2
-b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-G FILE
FILE exists and is owned by the effective group ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link (same as -h)
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and read permission is granted
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
FILE exists and write permission is granted
-x FILE
FILE exists and execute (or search) permission is granted
Except for -h and -L, all FILE-related tests dereference symbolic links. Beware that parentheses need to be escaped
(e.g., by backslashes) for shells. INTEGER may also be -l STRING, which evaluates to the length of STRING.
NOTE: [ honors the --help and --version options, but test does not. test treats each of those as it treats any other
nonempty STRING.
NOTE: your shell may have its own version of test and/or [, which usually supersedes the version described here. Please
refer to your shell's documentation for details about the options it supports.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report test translation bugs to <http://translation‐
project.org/team/>
AUTHOR
Written by Kevin Braunsdorf and Matthew Bradburn.
COPYRIGHT
Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
The full documentation for test is maintained as a Texinfo manual. If the info and test programs are properly installed
at your site, the command
info coreutils 'test invocation'
should give you access to the complete manual.
GNU coreutils 8.22 June 2018 TEST(1)
转载于:https://www.cnblogs.com/wangziyi0513/p/10720021.html
shell脚本if语句的多种条件参数相关推荐
- 【Linux】shell脚本实战-if多分支条件语句详解
文章目录 前言 多分支语句的语法 多分支语句举例: 1. 出嫁的条件 2. 管理http服务实战脚本 3. 猜数字游戏v1版本-if版本 总结 前言 前面我们已经学习过单分支语句和双分支语句的使用. ...
- shell 脚本循环语句
shell脚本循环语句 一.循环语句 1.1 for循环语句 1.2 for循环语法 1.3 for循环案例 1.4 类C风格循环 二.while语法 2.1 语法结构 2.2 while语法使用的一 ...
- 【Linux】shell脚本实战-if单双分支条件语句详解
文章目录 if单分支 1. 单分支语法分析 2. 单分支语法规范 if双分支语法分析 3. if单分支和双分支脚本举例 a. 检查是否为超级用户(双分支) b. 找一个金钱大于1000000的结婚(单 ...
- linux中脚本循环语句,Shell脚本循环语句
Shell作为一种脚本编程语言,同样了包含了循环,分支等其他程序控制结构,从而能够轻松完成更加复杂.强大的功能.我们今天就来认识if.for.while.case语句的具体应用. 一.使用if语句结构 ...
- Shell脚本循环语句及exit、continue和break用法
目录 一.for循环语句 二.while循环语句结构 三.until 循环语句结构 四.exit.continue和break 一.for循环语句 用法: 读取不同的变量值,用来逐个执行同一组命令 f ...
- shell脚本——循环语句
shell脚本--编程之循环语句(for.while.until) 一.输出字符串或提取Shell变量的值--echo命令 二.for循环语句 三.while循环 while 猜数字小游戏 四.unt ...
- shell脚本常用语句用法笔记
脚本基本语句用法笔记 grep -i 查询时不区分大小写 -n打印匹配的行号 -v 打印不匹配的行 -AX包括每次匹配之后X行 -BX包括每次匹配之后X行 cat /etc/passwd |grep ...
- shell脚本if语句判断
if条件判断语句,通常以if开头,fi结尾,也可加入else或者elif进行多条件的判断 如下 : if (表达式) 语句1 else 语句2 fi if语句Shell脚本编程案例如下: #!/bin ...
- shell脚本——if语句
if语句的结构和使用 在简单的Shell脚本程序中,各条语句将按先后顺序依次执行,从而实现批处理的自动化过程. 然而,单一的顺序结构使得脚本过于机械化,不够"智能",难以处理更加灵 ...
- linux的shell脚本if语句,Shell脚本编程之判断语句
一.单分支if语句 格式:if 判断条件:then statement1 statement2 ... fi 二.双分支的if语句 格式:if 判断条件; then statement1 statem ...
最新文章
- m1芯片 服务器,m1服务器地址
- 基于MATLAB的交流电动机调速系统仿真
- MultCloud – 支持数据互传的网盘管理
- 医学图像填洞处理-image fill holes
- 如何做好公司固定资产管理?如何管理企业的固定资产
- 如何用两个栈实现一个队列
- 深度学习-22:信息论和信息熵
- Apollo Planning决策规划算法代码详细解析 (14):SPEED_BOUNDS_PRIORI_DECIDER
- EPIC的服务器稳定吗,epic国内有服务器吗(epic服务器在哪)
- pip安装出现Script file ‘D:\ProgramData\Anaconda3\Scripts\pip-script.py‘ is not present.
- 标准差和均方根误差的区别
- allegro库函数 c语言,[转载]Allegro之Dev-Cpp版上手指南
- 一文读懂高速互联的阻抗及反射(中)
- stm32增加IWDG(看门狗)功能
- 2014 计算机会议,2014计算机学科国际会议排名.doc
- 聚焦语音交互,引爆智能硬件——暨讯飞语音云沙龙杭州站成功举办
- .NET中使用ORACLE函数和过程并输出参数(2)
- 关于adrduino HX711库的解析
- php字符串的查找和替换
- debian-F4232H测试
热门文章
- 在Docker上搭建ELK+Filebeat日志中心
- 【转】Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
- Android] AndroidManifest.xml文件解析
- 诛仙2·末日与曙光》解禁公测
- KVM虚拟化崛起:你选Ubuntu还是Red Hat?
- [RHEL5企业级Linux服务攻略]--第11季 NIS服务全攻略
- 升45武器并不一定大黑铁 自然+13的飞魂 (传奇3G)
- 手动玩转虚拟地址到物理地址转化
- Android 四大组件学习之Activity四
- android 5.0论坛,Android 安卓5.0以下版本提权漏洞