test命令在shell脚本中的应用
来源:互联网 发布:qq群搜索排名优化 编辑:程序博客网 时间:2024/04/20 03:38
接触shell脚本后,发现test命令在脚本中的应用非常多。学习shell脚本test命令是必须要掌握的,而且他很容易被掌握。
test命令可以处理shell脚本的各类工作。它产生的不是一般的输出(文本内容等),而是可使用的退出状态(true和false)。test接受各种不同的参数,可控制它要执行哪一种测试。
test命令有两种模式:
第一种:可以通过man test查看:test [-OPTION] EXPRESSION
不带参数:
点击(此处)折叠或打开
- if test "$str1 = $str2"
- then
- ......
- fi
点击(此处)折叠或打开
- if test -x "$file"
- then
- ......
- fi
第二种:[‘空’[-OPTION] EXPRESSION‘空’] 注意左右方括号和测试表达式之间必须存在一个空格.
不带参数:
点击(此处)折叠或打开
- if [ "$str1 = $str2" ]
- then
- ......
- fi
点击(此处)折叠或打开
- if [ -z "$str1" -a -z "$str2" ]
- then
- ......
- fi
( 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
[-OPTION]参数如下(栽man test):
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)
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
FROM: http://blog.chinaunix.net/uid-28728968-id-4407312.html
0 0
- test命令在shell脚本中的应用
- shell中的test命令
- shell中的test命令
- shell脚本命令test详解
- LINUX SHELL中的TEST命令
- shell中的test测试命令
- LINUX SHELL中的TEST命令
- Linux test 脚本 shell 命令详解
- Shell脚本的test命令用法
- shell脚本编程<三>:test命令
- Shell脚本test命令使用总结
- shell脚本之test命令(六)
- shell脚本中的sort命令
- shell脚本中的命令替换
- shell脚本中的命令代换
- Shell脚本中的trap应用
- Shell脚本中的trap应用
- shell语法中的test命令用法
- 在Makefile中无缝连接字符串
- iOS开发分分钟搞定C语言 —— 运算符
- 通过反射创建单例模式的另一个实例
- 克鲁斯卡尔算法(Kruskal)
- Spark修炼之道(基础篇)——Linux大数据开发基础:第十四节:Shell编程入门(六)
- test命令在shell脚本中的应用
- 如果饭店不是自己的,最好别去
- UI 实现多线程方式之四 GCD之函数作为参数
- java程序防止sql注入的方法
- 一道受用终身的测试题
- Reading <Effective Java>
- iOS编程------SQLite / 数据库
- Sublime Text 3使用方法简介
- 黑马程序员——C语言日志——static和extern