Shell编程中括号判断中赋值语句和判断语句
#!/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编程中括号判断中赋值语句和判断语句的更多相关文章
- 【转】shell编程下 特殊变量、test / [ ]判断、循环、脚本排错
[转]shell编程下 特殊变量.test / [ ]判断.循环.脚本排错 第1章 shell中的特殊变量 1.1 $# $# 表示参数的个数 1.1.1 [示例]脚本内容 [root@znix ~] ...
- shell编程系列6--shell中的函数
shell编程系列6--shell中的函数 .函数介绍 linux shell中的函数和大多数编程语言中的函数一样 将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 第一种格式 name() ...
- shell编程系列7--shell中常用的工具find、locate、which、whereis
shell编程系列7--shell中常用的工具find.locate.which.whereis .文件查找之find命令 语法格式:find [路径] [选项] [操作] 选项 -name 根据文件 ...
- shell编程下 特殊变量、test / [ ]判断、循环、脚本排错
第1章 shell中的特殊变量 1.1 $# $# 表示参数的个数 1.1.1 [示例]脚本内容 [root@znix ~]# cat /server/scripts/show2.sh #!/bin/ ...
- GCC:条件判断中赋值语句和函数结尾时无返回值的警告
有下面非常经典的一个字符串复制程序. test1.c #include <stdio.h> int main() { char str_t[]="This String come ...
- Linux Shell编程基础
在学习Linux BASH Shell编程的过程中,发现由于不经常用,所以很多东西很容易忘记,所以写篇文章来记录一下 ls 显示当前路径下的文件,常用的有 -l 显示长格式 -a 显示所有包括隐 ...
- Shell编程笔记
Shell编程笔记与Windows下熟悉的批处理类似,也可以将一些重复性的命令操作写成一个脚本方便处理. 修改别人的脚本,运行后遇到个问题 setenv: command not found 查证 ...
- shell编程学习笔记(八):Shell中的if条件判断
编程语言中都有条件判断,shell编程也不例外,下面我们来看一下shell中应该怎么使用if条件判断 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts ...
- shell if条件判断中:双中括号与单中括号的区别
电脑重装了系统,登录虚拟机的shell脚本需重写,在为编写的脚本命名时发现存在同名脚本,才想起来是连接公司服务器的登录脚本,不想写俩脚本,怕记混了,那就整合一下.代码如下: #!/bin/bash#z ...
随机推荐
- win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件
win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件 我杀过 w3wp.exe和asp.net_state的进程,重启 iis admin的服务,都还是不行. 只是把 ...
- Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
使用命令关闭占用80端口的程序 sudo fuser -k 80/tcp
- css整理-06 表和列表
表格式化 表布局 table, display:table caption, display: table-caption thead, display: table-header-group tbo ...
- express-17 持久化
简介 所有网站和Web应用程序(除了最简单的)都需要某种持久化方式,即某种比易失性内存更持久的数据存储方式,这样当遇到服务器宕机.断电.升级和迁移等情况时数据才能保存下来. 文件系统持久化 实现持久化 ...
- express-15 与生产相关的问题
执行环境 Express支持执行环境的概念,它是一种在生产.开发或测试模式中运行应用程序的方法.实际上你可以按自己的想法创建很多种不同的环境. 要记住,开发.生产和测试是"标准"环 ...
- 5.19[bzoj树网的核]
围观了final,SJTU还是飞了,泽民同志劲啊! 膜拜归膜拜...回来开题 bzoj1999树网的核 最近就喜欢给自己找切不动的题...QAQ ok.....昨天在家里做了一个下午+晚上 又困&am ...
- Codeforces Round #336 (Div. 2)
水 A - Saitama Destroys Hotel 简单的模拟,小贪心.其实只要求max (ans, t + f); #include <bits/stdc++.h> using n ...
- MFC 程序以管理员权限运行
首先,VS打开项目的属性 然后设置如图: 转载自:http://www.cnblogs.com/zzuhjf/archive/2012/09/12/2681548.html
- oracle sql别名
为sql的字段起一个别名,常以为是可有可无的,但是有时候是必要的. 例如在ibatis中 <!-- 获取已发或待发送的彩信记录列表 --> <resultMap id="m ...
- keytool创建Keystore和Trustsotre文件
一.生成一个含有一个私钥的keystore文件 user@ae01:~$ keytool -genkey -keystore keystore -alias jetty-azkaban -keyalg ...