bash shell test条件测试[[ ]]和[ ]异同小结
来源:互联网 发布:linux怎么看内存大小 编辑:程序博客网 时间:2024/05/19 13:17
test 命令非常强大,但是很难满足其转义需求以及字符串和算术比较之间的区别。
字符串比较
if [ "$a" == "$b" ]if [ "$a" != "$b" ]if [[ "$a" > "$b" ]]if [[ "$a" < "$b" ]]if [[ "$a" = "$b" ]]
if [[ "$a" == "$b" ]]if [[ "$a" != "$b" ]]
[[ ]] and []
功能[[ ]][ ]例子字符串比较>\> -=(or ==)= -!=!= -表达式组合&&-a[[ -n $var && -f $var ]] && echo “$var is a file”||-o -
(Pattern matching)
特性
例子
文件或文件夹存在
-e
[[ -e $config ]] && echo ”config file exists: $config”
文件新旧比较
-nt/-ot
[[ $file0 -nt $file1 ]] && echo ”$file0 is newer than $file1″
同一个文件
-ef
[[ $input -ef $output ]] \ && { echo ”will not overwrite input file: $input”; exit 1; }
否定
!
-
逻辑组合判断的一些例子:
if [ $condition1 ] && [ $condition2 ]
if [ $condition1 -a $condition2 ]
if [[ $condition1 && $condition2 ]]
if [ $condition1 ] || [ $condition2 ]
if [ $condition1 -o $condition2 ]
if [[ $condition1 || $condition2 ]] # Also works.
#The &&, ||, operators work within a [[ ]] test, despite giving an error within a [ ] construct.
字符串比较
if [ "$a" == "$b" ]if [ "$a" != "$b" ]if [[ "$a" > "$b" ]]if [[ "$a" < "$b" ]]if [[ "$a" = "$b" ]]
if [[ "$a" == "$b" ]]if [[ "$a" != "$b" ]]
[[ ]] and []
功能[[ ]][ ]例子字符串比较>\> -=(or ==)= -!=!= -表达式组合&&-a[[ -n $var && -f $var ]] && echo “$var is a file”||-o -
(Pattern matching)
特性
例子
文件或文件夹存在
-e
[[ -e $config ]] && echo ”config file exists: $config”
文件新旧比较
-nt/-ot
[[ $file0 -nt $file1 ]] && echo ”$file0 is newer than $file1″
同一个文件
-ef
[[ $input -ef $output ]] \ && { echo ”will not overwrite input file: $input”; exit 1; }
否定
!
-
逻辑组合判断的一些例子:
if [ $condition1 ] && [ $condition2 ]
if [ $condition1 -a $condition2 ]
if [[ $condition1 && $condition2 ]]
if [ $condition1 ] || [ $condition2 ]
if [ $condition1 -o $condition2 ]
if [[ $condition1 || $condition2 ]] # Also works.
#The &&, ||, operators work within a [[ ]] test, despite giving an error within a [ ] construct.
- bash shell test条件测试[[ ]]和[ ]异同小结
- shell条件表达式小结:test([])与 [[]]
- Shell脚本语法 条件测试:test
- Shell 学习(四、条件测试(test,[]) 和 条件判断(if..then..fi))
- bash shell if-test
- BASH TEST 测试操作
- Bash测试----test, [], [[]], (())
- 关于makefile和shell脚本的异同:make和bourne again shell--bash
- 04 shell条件测试和判断语句
- SHELL条件测试和流程控制
- shell之条件测试和判断语句
- Bash Shell脚本学习小结
- Bash Shell(字符串)操作小结
- Bash Shell字符串操作小结
- Bash Shell字符串操作小结
- Bash Shell字符串操作小结
- Bash Shell脚本学习小结
- Bash Shell脚本学习小结
- 如何写出好的 JavaScript —— 浅谈 API 设计
- ReacOS源代码阅读之驱动--IoCreateDevice
- 【JZOJ3808】道路值守
- tomcat解析jsp错误
- jQuery-ui datepicker 日历选择器
- bash shell test条件测试[[ ]]和[ ]异同小结
- C++开发工程师课程第一周笔记 GeekBank
- 最短路模板
- SQL注入这个坑
- ECharts地图应用定位
- 关于PopupWindow的简单说明
- 微信小程序创建时:当前开发者未绑定此appid,请到mp后台操作
- iOS转场动画总结
- 学习HTML第一天