#!/bin/bash

declare var="xxx"
# without space and use one =
#1.judge whether the assignment statement returns true
echo "----------------------test assignment in bracket --------------------------------"
[ var="yyyy" ] && echo "success"
echo "end"
# success
[ "$var"="yyyy" ] && echo "success"
echo "end"
# success
[ "var"="yyyy" ] && echo "success"
echo "end"
# success
[ var1="yyyy" ] && echo "success"
echo "end"
# success # result: yes #2. judge the assignment whether be taken in bracket
echo "--------------------test assignment whether been taken -----------------------"
[ var="yyyy" ] && echo "$var"
echo "end"
#xxx
#end
#result: assignment can't be taken in bracket #3. whether the effect of single = and double = similary
echo "-------------------test single = and double = ------------------------"
[ "$var" == "yyyy" ] && echo "$var"
echo "end"
#end
[ "$var" = "yyyy" ] && echo "$var"
echo "end"
#end
var="yyyy"
[ "$var" == "yyyy" ] && echo "$var"
echo "end"
#yyyy
#end
[ "$var" = "yyyy" ] && echo "$var"
echo "end"
#yyyy
#end
# result: effect same
#4. whether the effect of having space and not space similary
echo "----------------------test space -------------------------------------"
[ "$var"=="yyyy" ] && echo "$var"
echo "end"
#yyyy
#end
[ "$var"=="zzzz" ] && echo "$var"
echo "end"
#yyyy
#end
#result: always true with no space #conclusion:
# with space at both side of = will judge in normal
# don't space at both side of = or == will always true

通过上面的测试得出的结论是:

在中括号判断中不使用空格例如  "$var"="xx" 或 "$var"=="xxx"(在= 和== 两边没有空格)则这个判断一直是真

在中括号中使用空格 例如  "$var" = "xx" 或 "$var" == "xxx"(在= 和== 两边有空格) 则这个判断会按照正常的值进行判断,值相等返回true否则返回false

可能会因使用的shell及其版本不同而导致结果不同,我使用的shell是:GNU bash, version 4.1.2(1)-release (i686-redhat-linux-gnu)

Shell编程中括号判断中赋值语句和判断语句的更多相关文章

  1. 【转】shell编程下 特殊变量、test / [ ]判断、循环、脚本排错

    [转]shell编程下 特殊变量.test / [ ]判断.循环.脚本排错 第1章 shell中的特殊变量 1.1 $# $# 表示参数的个数 1.1.1 [示例]脚本内容 [root@znix ~] ...

  2. shell编程系列6--shell中的函数

    shell编程系列6--shell中的函数 .函数介绍 linux shell中的函数和大多数编程语言中的函数一样 将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 第一种格式 name() ...

  3. shell编程系列7--shell中常用的工具find、locate、which、whereis

    shell编程系列7--shell中常用的工具find.locate.which.whereis .文件查找之find命令 语法格式:find [路径] [选项] [操作] 选项 -name 根据文件 ...

  4. shell编程下 特殊变量、test / [ ]判断、循环、脚本排错

    第1章 shell中的特殊变量 1.1 $# $# 表示参数的个数 1.1.1 [示例]脚本内容 [root@znix ~]# cat /server/scripts/show2.sh #!/bin/ ...

  5. GCC:条件判断中赋值语句和函数结尾时无返回值的警告

    有下面非常经典的一个字符串复制程序. test1.c #include <stdio.h> int main() { char str_t[]="This String come ...

  6. Linux Shell编程基础

    在学习Linux BASH Shell编程的过程中,发现由于不经常用,所以很多东西很容易忘记,所以写篇文章来记录一下 ls   显示当前路径下的文件,常用的有 -l 显示长格式  -a 显示所有包括隐 ...

  7. Shell编程笔记

    Shell编程笔记与Windows下熟悉的批处理类似,也可以将一些重复性的命令操作写成一个脚本方便处理.   修改别人的脚本,运行后遇到个问题 setenv: command not found 查证 ...

  8. shell编程学习笔记(八):Shell中的if条件判断

    编程语言中都有条件判断,shell编程也不例外,下面我们来看一下shell中应该怎么使用if条件判断 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts ...

  9. shell if条件判断中:双中括号与单中括号的区别

    电脑重装了系统,登录虚拟机的shell脚本需重写,在为编写的脚本命名时发现存在同名脚本,才想起来是连接公司服务器的登录脚本,不想写俩脚本,怕记混了,那就整合一下.代码如下: #!/bin/bash#z ...

随机推荐

  1. POJ 1971 统计平行四边形 HASH

    题目链接:http://poj.org/problem?id=1971 题意:给定n个坐标.问有多少种方法可以组成平行四边形.题目保证不会有4个点共线的情况. 思路:可以发现平行四边形的一个特点,就是 ...

  2. 《DSP using MATLAB》示例Example5.3

  3. scau 8637 阶乘与因子 筛素数

    时间限制:500MS  内存限制:1000K提交次数:189 通过次数:46 题型: 编程题   语言: G++;GCC Description 游戏玩了很久总会厌的,连Lyd的蚂蚁都被放生了.... ...

  4. 转 Delphi中使用FastMM4结合View CPU避免内存泄漏

    http://www.cnblogs.com/kongchao/archive/2009/10/27/1590479.html 核心提示:内存泄漏经常出现在本地代码中,特别是多线程和发生异常的情况下, ...

  5. 记一次mybatis的classpath踩坑记录

    http://blog.csdn.net/jinzhencs/article/details/50595476

  6. gridview自定义排序

    效果如图: 首先允许排序:AllowSorting="True":开启gridview的排序事件onsorting="GridView1_Sorting",也可 ...

  7. BestCoder#16 A-Revenge of Segment Tree

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  8. ural 1144. The Emperor's Riddle

    1144. The Emperor's Riddle Time limit: 1.0 secondMemory limit: 4 MB Background In the olden times th ...

  9. BZOJ2285 : [Sdoi2011]保密

    首先通过分数规划,二分答案$mid$,将每条边边权重置为$t-mid\times s$,用DP求出终点到该点的最短路,若非正则可以更小. 如此可以计算出每个出入口的最小危险值,然后把奇点放在$S$,偶 ...

  10. web移动端性能调优及16ms优化

    本文只是一个索引,收集了网络上大部分关于调试及优化方面的文章,从中挑选了一些比较好的文章分享给大家. 移动端性能不及桌面浏览器性能的10分之1,特别是在android设备良莠不齐的情况下,性能显得尤为 ...