[本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020]
变量和替换操作,在脚本中执行变量替换时最容易犯的一个错误就是引用错误,所以有必要看一下各种引号的代表的意义与作用。
使用双引号可引用除字符$、`、\ 外的任意字符或字符串。如下所示:
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo -e "hello $SHELL '\n* jesson`echo 20121020`" hello /bin/bash ' * jesson20121020
单引号与双引号类似,不同的是shell会忽略任何引用值。换句话说,如果屏蔽了其特殊含义(不会屏蔽\),则会将引号里的所有字符,包括引号都作为一个字符串。
还是以上个例子来说明,我们这次将外面的双引号换成单引号。
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo -e 'hello $SHELL \n* jesson`echo 20121020`' hello $SHELL * jesson`echo 20121020`可以看出,除了\n,其他的特殊符号全被屏蔽了。
反引号用于设置系统命令的输出到变量。shell将反引号中的内容作为一个系统命令,并执行其内容。
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo "hello `echo 20121020`" hello 20121020
反斜杠可以屏蔽特殊含义。下述字符包含有特殊意义:& * + ^ $ ` " | ?。
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo * helloworld.sh parm.sh searchfile.sh jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo \* *
运算符是对计算机发出的指令
- 数字,字符
- 变量
- 表达式:运算符和运算对象的组合体
&[ ] 告诉shell对方括号中的表达式求值。
~(反运算符),<<(左移) ,>>(右移),&(与),|(或),^(异或)
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ ~2 ] -3 jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 2<<1 ] 4 jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 2>>1 ] 1 jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 2&3 ] 2 jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 2|3 ] 3 jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 2^3 ] 1
&&(逻辑与),||(逻辑或),>,==,<,!=
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 1&1 ] 1 jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 1&0 ] 0 jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 1|0 ] 1 jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 1|1 ] 1 jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 0|0 ] 0
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=
let $count=$count + $change
let $count+=$change
jesson@jesson-HP:~/develop/workspace/shell_workspace$ var=10 jesson@jesson-HP:~/develop/workspace/shell_workspace$ let var+=4 jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $var 14
$[ ] 和$(()) 用于表达式求值。
$[ ]可以接受不同基数的数字:
-[ base#n ] n表示基数从2到36的任何基数。
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $((8+6)) 14 jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[10#8+1] 9
linux学习之shell脚本 ------- 引用与运算符
原文地址:http://blog.csdn.net/jesson20121020/article/details/42807291