http://blog.csdn.net/vah101/article/details/6173488

( a=2;b=4;c=9; ) 子shell 环境

{ a=2;b=4;c=9; } 当前shelll环境

((整型算术表达式 )) 返回0 /1

[[条件表达试]] 0/1

[] 0/1

$(命令)= `命令` 返回结果

[root@monitor ~]# b=`date`
[root@monitor ~]# echo $b
Mon May 23 23:59:44 CST 2016

[root@monitor ~]# a=$(date)
[root@monitor ~]# echo $a
Mon May 23 23:59:01 CST 2016

$(())

[root@monitor ~]# echo $((2,4,5))
5


[[ ]] [] :常量运算符恒为真   

[root@monitor ~]# [[   >  ||   ]]
[root@monitor ~]# echo $? [root@monitor ~]# [[ > || ]]
[root@monitor ~]# echo $? [root@monitor ~]# [[ > ]]
[root@monitor ~]# echo $?

root@monitor ~]# [ 1 -a 2 ]
[root@monitor ~]# echo $?
0
[root@monitor ~]# [ 1 -a 0 ]
[root@monitor ~]# echo $?
0
[root@monitor ~]# [ 1 -o 0 ]
[root@monitor ~]# echo $?
0


command1 & command2 & command3     三个命令同时执行
command1; command2; command3 不管前面命令执行成功没有,后面的命令继续执行
command1 && command2 只有前面命令执行成功,后面命令才继续执行
[root@monitor ~]# ((  && ))
[root@monitor ~]# echo $?
[root@monitor ~]#  type cat
cat is /bin/cat
[root@monitor ~]# type [
[ is a shell builtin
[root@monitor ~]# type [[
[[ is a shell keyword []:中的逻辑符号 -o -a
[[ ]] :中的逻辑符号 && ||
(( ))

[root@monitor ~]# (( && ))
[root@monitor ~]# echo $? [root@monitor ~]# (( && > ))
[root@monitor ~]# echo $? [root@monitor ~]# (( && ))
[root@monitor ~]# echo $?
(())结构计算并测试算数表达式的结果,退出码与[]相反

[true返回0,false返回1]

(())                       #返回1

(())                       #返回0

((>))                     #返回0

((>))                     #返回1

((-))                     #返回1

((/))                    #大于1,返回0

((/))                    #小于1,返回1

((/))                    #报错,返回1

[root@monitor ~]# ((0))
[root@monitor ~]# echo $?
1

[root@monitor ~]# [ 0 ]
[root@monitor ~]# echo $?
0

[root@monitor ~]# [[ 0 ]]
[root@monitor ~]# echo $?
0

文件测试操作:

返回true,如果:

-e                          文件存在

-a                          文件存在(已被弃用)

-f                          被测文件是一个regular文件(正常文件,非目录或设备)

-s                          文件长度不为0

-d                          被测对象是目录

-b                          被测对象是块设备

-c                          被测对象是字符设备

-p                          被测对象是管道

-h                          被测文件是符号连接

-L                          被测文件是符号连接

-S(大写)                     被测文件是一个socket

-t                          关联到一个终端设备的文件描述符。用来检测脚本的stdin[-t0]或[-t1]是一个终端

-r                          文件具有读权限,针对运行脚本的用户

-w                          文件具有写权限,针对运行脚本的用户

-x                          文件具有执行权限,针对运行脚本的用户

-u                          set-user-id(suid)标志到文件,即普通用户可以使用的root权限文件,通过chmod +s file实现

-k                          设置粘贴位

-O                          运行脚本的用户是文件的所有者

-G                          文件的group-id和运行脚本的用户相同

-N                          从文件最后被阅读到现在,是否被修改

f1 -nt f2                   文件f1是否比f2新

f1 -ot f2                   文件f1是否比f2旧

f1 -ef f2                   文件f1和f2是否硬连接到同一个文件

二元比较操作符,比较变量或比较数字

整数比较:

-eq                       等于            if [ "$a" -eq "$b" ]

-ne                       不等于          if [ "$a" -ne "$b" ]

-gt                       大于            if [ "$a" -gt "$b" ]

-ge                       大于等于         if [ "$a" -ge "$b" ]

-lt                       小于            if [ "$a" -lt "$b" ]

-le                       小于等于        if [ "$a" -le "$b" ]

<                         小于(需要双括号)       (( "$a" < "$b" ))

<=                        小于等于(...)           (( "$a" <= "$b" ))

>                         大于(...)               (( "$a" > "$b" ))

>=                        大于等于(...)           (( "$a" >= "$b" ))

字符串比较:

=                         等于                   if [ "$a" = "$b" ]

==                        与=等价

!=                        不等于                  if [ "$a" != "$b" ]

<                         小于,在ASCII字母中的顺序:

                          if [[ "$a" < "$b" ]]

                          if [ "$a" /< "$b" ]         #需要对<进行转义

>                         大于

-z                        字符串为null,即长度为0

-n                        字符串不为null,即长度不为0

注意:

使用-z或-n判断字符串变量时,必须要用""把变量引起来。

例如:

if [ -n $string1 ]  #string1未被初始化

then

    echo "String /"string1/" is not null."

else

    echo "String /"string1/" is null"

fi

#结果显示string1为非空,错误

if [ -n "$string1" ]    #string1仍然未被初始化

then

    echo "String /"string1/" is not null"

else

    echo "String /"string1/" is null"

fi

#结果显示string1为空,结果正确

if [ $string1 ]          #string1裸体判断

then

    echo "String /"string1/" is not null"

else

    echo "String /"string1/" is null"

fi

#结果正确

#但这种用法存在漏洞,比如:

string1="1 > 2"

if [ $string1 ]

then

    echo "String /"string1/" is not null"

else

    echo "String /"string1/" is null"

fi

#实际上[]中的内容被扩展为[ "1 > 2" ],所以结果会出错。
而使用[[ $string1 ]],则可以避免错误
需要转义

[root@monitor ~]# [ "a" \> "b" ]
[root@monitor ~]# echo $? [root@monitor ~]# [ "b" \> "a" ]
[root@monitor ~]# echo $? [root@monitor ~]# [ -a ]
[root@monitor ~]# echo $? [root@monitor ~]# [ -a ]
[root@monitor ~]# echo $? [root@monitor ~]# [ -o ]
[root@monitor ~]# echo $? [root@monitor ~]# [ -o ]
[root@monitor ~]# echo $? [root@monitor ~]# [[ "a" > "b" ]]
[root@monitor ~]# echo $? [root@monitor ~]# [[ "a" < "b" ]]

SHELL 详解的更多相关文章

  1. Linux的Bash Shell详解

    一.Bash Shell概述 1.什么是bash         bash是Bourne Again Shell的简称,是从unix系统中的sh发展而来的,是用户和Linux内核交互的工具,用户通过b ...

  2. hadoop shell 详解

    概述  所有的hadoop命令均由bin/hadoop脚本引发.不指定参数运行hadoop脚本会打印所有命令的描述.  用法: hadoop [--config confdir] [COMMAND] ...

  3. 每篇半小时1天入门MongoDB——3.MongoDB可视化及shell详解

    本篇主要介绍MongoDB可视化操作以及shell使用及命令,备份恢复.数据导入导出. MongoVUE安装和简单使用 使用mongo.exe 管理数据库虽然可行,功能也挺强大,但每次都要敲命令,即繁 ...

  4. Impala shell详解

    不多说,直接上干货! 查看帮助文档 impala-shell -h 刷新整个云数据 impala-shell -ruse impala;show tables; 去格式化,查询大数据量时可以提高性能 ...

  5. Linux之shell详解

    Shell是什么 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言.Shell 是指一种应用程序,这个应用程序提供了一个界 ...

  6. Shell—详解$0、$1、$2、$#、$*、$@、$?、$$变量

    预定义变量:常用来获取命令行的输入 变量 作用 $0 当前Shell脚本本身的文件名称 $1 脚本接收的第一个参数($1-$9:第1-9个命令行参数名) $2 脚本接收的第二个参数($1-$9:第1- ...

  7. Shell [[]]详解:检测某个条件是否成立

    [[ ]]是 Shell 内置关键字,它和 test 命令类似,也用来检测某个条件是否成立. test 能做到的,[[ ]] 也能做到,而且 [[ ]] 做的更好:test 做不到的,[[ ]] 还能 ...

  8. Shell—详解$( )、$(( ))、``与${ }的区别

    https://www.jianshu.com/p/2237f029c385 https://www.cnblogs.com/chenpython123/p/11052276.html https:/ ...

  9. 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器

    本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...

随机推荐

  1. linux 命令——文件管理 cat

    一.介绍 cat 是一个文本文件查看和连接工具.从第一个字节开始正向查看文件的内容. 主要有三大功能: 1.一次显示整个文件.$ cat   filename ~,y6;e2.从键盘创建一个文件.$ ...

  2. Tableau学习笔记之一

    书本:Tableau数据可视化实战,Ashutosh Nandeshwar著 学习时主要采用Tableau Desktop 9.0,由于该软件是商业软件,价格不菲,故只能试用,期限为14天,可以通过修 ...

  3. Linux时间与Windows差8个时区的问题解决方法

    我的Debian7.1的时间与windows上的时间不一致,正好差8个时区,原因是Debian将机器的物理时间理解为UTC时间了.去网上找了好多文章,基本上都是说要改/etc/default/rcS, ...

  4. Mahout分步式程序开发 聚类Kmeans(转)

    Posted: Oct 14, 2013 Tags: clusterHadoopkmeansMahoutR聚类 Comments: 13 Comments Mahout分步式程序开发 聚类Kmeans ...

  5. support vector regression与 kernel ridge regression

    前一篇,我们将SVM与logistic regression联系起来,这一次我们将SVM与ridge regression(之前的linear regression)联系起来. (一)kernel r ...

  6. bzoj 3218 a + b Problem(最小割+主席树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3218 [题意] 给n个格子涂白或黑色,白则wi,黑则bi的好看度,若黑格i存在: 1& ...

  7. 使用Async同步执行异步函数

    为了适应异步编程,减少回调的嵌套,我在项目中引入了Async,当批量处理且需要同步执行一些逻辑相同的异步函数时,觉得还是Async最为靠谱. 我有一个类似下面代码的场景,依据数组中的每一个元素执行一个 ...

  8. HDU-4749 Parade Show KMP算法 | DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题意:给两个串S和P,求S串中存在多少个与P串的大小关系一样的串. 因为数字的范围是1<= ...

  9. post和put的区别

    post : 标识的是处理请求中资源表述的资源 put :  标识的是请求的资源表述 post : 支持多种类型的资源 put  : 支持单一的资源

  10. Native libraries .so.XY failing to link at runtime

    What you need to do is edit the configure file. And find out this: SLIBNAME='$(SLIBPREF)$(FULLNAME)$ ...