if的[]和[[]]
来源:互联网 发布:python thinter视频 编辑:程序博客网 时间:2024/06/05 06:11
[]是bash里test的同义词,比如[ -d filename ]和test -d filename的结果是一样的,逻辑测试使用-a、-o
[[]]比[]通用,逻辑测试使用&&、||
#!/bin/bashx=$1if [ -d $x ];then echo okelse echo "not equel"fi----------------------------[root@localhost shelltest]# test -d /root && echo llll
在[]里面,使用-eq的时候,需要用整数来做参数,如果是非整数就会提示报错,而[[]]则直接把非整数的字符串转成了0(),而不会去检查并显示报错内容。
当if [ $x -eq $y ]时[root@localhost shelltest]# ./teststring.sh 1 ad./teststring.sh: line 4: [: ad: integer expression expected当if [[ $x -eq $y ]]时[root@localhost shelltest]# ./teststring.sh ad adok[root@localhost shelltest]# ./teststring.sh 1 anot equel[root@localhost shelltest]# ./teststring.sh aa 0ok[root@localhost shelltest]# ./teststring.sh 0 aaok
[]和[[]]都不支持+-*/数学运算符
整数的比较
注意前面都是有个符号-
eq等于ne不等于gt大于ge大于等于lt小于le小于等于例if [ $x -eq $y ]if [[ $x -gt $y ]]if [ "$x" -eq "$y" ]if [[ "$x" -eq "$y" ]]
>大于>=大于等于<小于<=小于等于例if [[ $x > $y ]]if (( $x > $y ))
注:if [ $x > $y ]会一直为true,可以改成if [ $x \> $y ],也就是把符号>转义(包括字符串(**ASCII中对应的顺序大小**)和整数)。(if [ "$x" \> "$y" ])
字符串的比较
=等于,效果和==是一样的
例
if [ $x = $y ]if [[ $x = $y ]]if [ $x == $y ]if [[ $x == $y ]]if [[ $x = "abc" ]]
-z测试是否为空,为空则为true
if [ -z "$x" ]if [ -z $x ]
-n测试是否不为空,不为空则为true
if [ -n "$x" ]
注:需要有双引号,负责一直为true
阅读全文
0 0
- if......if和if......else if......else if的区别
- if...if 和if...else if...的区别
- if的[]和[[]]
- if 和 if...else的意义
- if 和 else if 的效率问题
- if和else if的用法
- if和else if的使用注意事项
- if 和else if 的打法
- if else if 和多个if的区别.
- 关于if if和if elseif的区别
- 简述if if和else if的关系与区别
- assert 和 if 的区别
- if和whether的区别
- #if 和#ifdef的区别
- #ifdef和#if的差别
- ifelse和if的区别
- if和switch的应用
- #ifdef和#if的差别
- Struts2 文件上传 下载
- Atitit it 互联网 软件牛人的博客列表
- Cookie/Session详解
- 用word发CSDN blog,免去插图片的烦恼
- 转发和重定向的比较
- if的[]和[[]]
- 2017CCPC网络选拔赛1005CaoHaha's staff(规律贪心)
- Codeforces Round #424 E. Cards Sorting
- web运用程序在web容器加载顺序
- 基于redis的延迟消息队列设计
- C++求矩阵最小路径和进阶方法空间复杂度O(min {row, col})
- JVM(1):Java 类的加载机制
- A Secret 2017 CCPC 网络选拔赛 hdu 6153
- 日常培训一些记录