天天看点

【转载】shell中的条件判断、参数以及变量替换

【test 命令】 

      test 命令是 shell 的内部命令,用于计算作为其参数的表达式是真还是假。 如果表达式为真,则 test 返回0(注意与 c 语言 0 表示 false 不同),如果表达式为假,返回非 0 。 

      shell 提供了另外一个调用 test 命令的方式,就是用方括号 [ ] 来代替单词 test 。 

      调用 test 命令的格式为: 

<a href="http://my.oschina.net/moooofly/blog/170629#">?</a>

1

2

<code>test</code> <code>expr1 opr expr2 或是 [ expr1 opr expr2 ]</code>

<code>test</code> <code>opr expr1 或是 [ opr expr1 ]</code>

(注意方括号与表达式之间的空格必须存在) 

【逻辑运算符】 

      -a      expr1 -a expr2      逻辑与 

      -o      expr1 -o expr2      逻辑或 

        !      !expr1                    逻辑非 

【数值判断】 

      -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" ]       是否小于等于

      &lt;         (("$a" &lt; "$b"))       小于(需要双括号) 

      &lt;=      (("$a" &lt;= "$b"))     小于等于(需要双括号) 

      &gt;        (("$a" &gt; "$b"))        大于(需要双括号) 

      &gt;=      (("$a" &gt;= "$b"))     大于等于(需要双括号) 

【字符串判断】 

      =       if [ "$a" = "$b" ]        字符串是否相等 

      !=      if [ "$a" != "$b" ]       字符串是否不等 

      -n      if [ -n str1 ]                字符串长度是否不等于 0 

      -z      if [ -z str2 ]                 字符串长度是否等于 0 

      ==      if [ "$a" == "$b" ]     与 = 等价(但有例外)。 

【文件判断】 

      -b file      若文件存在且是一个块特殊文件

      -c file      若文件存在且是一个字符特殊文件

      -d file      若文件存在且是一个目录

      -e file      若文件存在

      -f file      若文件存在且是一个规则文件(普通文件)

      -g file      若文件存在且设置了sgid位的值

      -h file      若文件存在且为一个符合链接

      -k file      若文件存在且设置了”sticky”位的值

      -p file      若文件存在且为一已命名管道

      -r file      若文件存在且可读

      -s file      若文件存在且其大小大于零

      -u file      若文件存在且设置了suid位

      -w file      若文件存在且可写

      -x file      若文件存在且可执行

      -o file      若文件存在且被有效用户id所拥有

【命令行参数/位置变量】 

      $0      脚本的名字 

      $1, $2, ..., $9      脚本第 1 个到第 9 个命令行参数 

      $#      命令行参数的个数 

      $@ 或是 $*      所有命令行参数 

      $?      最后一条命令的退出状态 

      $$      正在执行进程的 id(pid) 

给位置变量赋值: 

3

4

<code>[root@betty ~]</code><code># set one two tree</code>

<code>[root@betty ~]</code><code># echo $1 $2 $3</code>

<code>one two tree</code>

<code>[root@betty ~]</code><code>#</code>

【shift 命令】 

      这条命令可以对位置参数进行移动,每次调用都会导致:$1=$2、$2=$3、$3=$4.... 

      以下代码可以打印出所有的参数 

5

<code>while</code> <code>[ -n</code><code>"$1"</code> <code>]</code>

<code>do</code>

<code>    </code><code>echo</code> <code>$1</code>

<code>    </code><code>shift</code>

<code>done</code>

【变量替换(赋值)】 

      shell 提供了变量替换功能,使用户能够检查变量的值,并根据选项改变它的值。 

      $variable      保存在 variable 中的值 

      ${variable}      保存在 variable 中的值 

      ${variable:-string}      如果 variable 的值非空,则值为 variable,否则值为 string 

      ${variable:+string}      如果 variable 的值非空,则值为 string,否则值为空 

      ${variable:=string}      如果 variable 的值非空,则值为 variable,否则值为 string 且 variable 的值设置为string 

      ${variable:?string}      如果 variable 的值非空,则值为 variable,否则显示 string 并退出 

【正则表达中的特殊字符】 

行末:$       

行首:^ 

空格:\s 

行末空格:\s\+$ 

行首空格:^\+\s 

【注】 

      当在对变量进行判断时最好将变量用双引号括起来,这样可以避免参数包含空格或是 tab 带来的问题。如 " $home"、"$#" 等。 

      使用 [ expr ] 进行判定时,expr与 [ 及 ] 之间均必须有空格。 

       == 的功能在 [[ ]] 和 [] 中的行为是不同的 , 这个操作符将在 [[ ]] 结构中使用模式匹配 。 如下: 

[[ $a == z* ]]        # 如果 $a 以 "z" 开头(模式匹配)那么将为 true 

[[ $a == "z*" ]] # 如果 $a 等于 "z*" (字符匹配),那么结果为 true

[ $a == z* ]      # file globbing 和 word splitting 将会发生

[ "$a" == "z*" ] # 如果 $a 等于 z* (字符匹配),那么结果为 true

继续阅读