#!/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. directive中的参数详解

    restrictE: 表示该directive仅能以element方式使用,即:<my-dialog></my-dialog>A: 表示该directive仅能以attribu ...

  2. JQuery学习之jQuery尺寸

    1.width()和height()方法: width():设置或返回元素的宽度(不包括内边距,边框或外边距) height():设置或返回元素的高度(不包括内边距,边框或外边距) $("b ...

  3. psql-06表:约束

    默认值 可以理解为建表时没定义的默认值为null,表示未知,//注意和js中null不一样; 建表时设置 create table child(id int, age int default 18); ...

  4. js整理3

    函数 call: fun.call(a), a会转化成相应的对象,函数内的this即指向它; function foo() { console.log(this); } foo.call(null); ...

  5. Editthiscookie

    Editthiscookie,联调,.s环境加cookie才能访问.laravel

  6. Jenkins基础 - 常用配置操作

    1.修改jenkins的根目录,默认地在C:\Users\用户名\.jenkins下(win7) 或C:\Documents and Settings\用户名\.jenkins(xp) 修改步骤: 增 ...

  7. Python学习笔记02

      元组:圆括号的,不能进行赋值操作,即不可更改. 列表:方括号的,可以修改. 访问:均使用下标访问   # 元组是一个静态列表,初始化之后就不可以修改,可以试任意类型 tuple1 = ('a st ...

  8. jQuery入门第三天

    每个HTML元素根据继承属性都有父parent元素. 举个例子,h3 元素的父元素是 <div class="container-fluid">,<div cla ...

  9. Code Rush插件

    code rush 是微软推出的一款VS2008上的插件.他有强大的文件和代码导航功能,易于访问的重构和代码创建功能.一组编辑器.选择.剪贴板工具等. 教程链接 http://www.devexpre ...

  10. Android 魅族等SmartBar适配

    通过反射获取是否含有SmartBar: /** * 判断是否有SmartBar */ private boolean hasSmartBar() { // SP存储是否显示SmartBar if (! ...