#!/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. 开发Portlet第二步:如何将Crystal静态Portlet转变成基于测试数据的动态Portlet?

    当基于Crystal的静态Portlet开发完成后,在与后台服务联调前,还需要将Portlet转换成基于测试数据的动态Portlet.具体步骤如下: 分步指南 复制Portlet项目,并修改相关的po ...

  2. BootSrap学习

    1.当row1里边有2个或多个col的时候,如果位置在后边的col的高度大于位置在前边的col,就可能会导致下一个row2在满足col-xs-*在满足和为12的情况下错乱分行.解决办法是强行给row1 ...

  3. for的冒泡排序练习题

    这是一个冒泡排序的方法,请汲取其中的思想.有一组数: 1,2,3,4,5,6请将这组数用降序排列.我们可以将数组里面的数两两相比,如果第二个数比第一个数大,那么将第二个数值与第一个数值交换,然后让其循 ...

  4. iOS10 UI教程视图调试

    iOS10 UI教程视图调试 iOS10 UI教程视图调试,当视图很复杂的时候,层次结构就不会很简单了.Xcode可以通过视图(View)调试帮助开发者解决层次结构复杂的问题.视图调试是在Xcode ...

  5. 餐厅系统app7

    团队贡献分 杨子健:23 郭志豪:24 谭宇森:22 刘森松:31

  6. iOS UIImageView用代码添加点击事件

    image.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]ini ...

  7. HDU5834 Magic boy Bi Luo with his excited tree(树形DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...

  8. python程序设计语言笔记 第一部分 程序设计基础

    1.1.1中央处理器(CPU) cpu是计算机的大脑,它从内存中获取指令然后执行这些指令,CPU通常由控制单元和逻辑单元组成. 控制单元用来控制和协调除cpu之外的其他组件的动作. 算数单元用来完成数 ...

  9. jenkins 的 ProcessTreeKiller----无法启动子进程的解决办法

    参考: http://alanland.iteye.com/blog/2047244 http://scmbob.org/start-process-in-jenkins.html java -Dhu ...

  10. Codeforces Round #243 (Div. 2) C. Sereja and Swaps

    由于n比较小,直接暴力解决 #include <iostream> #include <vector> #include <algorithm> #include ...