`
810364804
  • 浏览: 781950 次
文章分类
社区版块
存档分类
最新评论

shell 编程中空格的使用,双引号,单引号,反引号

 
阅读更多

http://blog.csdn.net/panda19881/article/details/6626727

1.定义变量时, =号的两边不可以留空格.
eg:
gender=femal————right
gender =femal———–wrong
gender= femal———–wrong
2.条件测试语句 [ 符号的两边都要留空格.
eg:
if [ $gender = femal ]; then——-right.
echo “you are femal”;
fi

if[ $gender...-----------------------wrong
if [$gender...----------------------wrong.

3.条件测试的内容,如果是字符串比较的话, 比较符号两边要留空格!
eg:
if [ $gender = femal ]; then——-right.
if [ $gender= femal ]; then——–wrong.
if [ $gender=femal ]; then———wrong.

4.如果if 和 then写在同一行, 那么,注意, then的前面要跟上 ; 号.如果 then 换行写, 那么也没问题.
eg:
if [ $gender = femal ]; then——-right.
if [ $gender = femal ]
then——————————-right.
if [ $gender = femal ] then——-wrong. then前面少了 ; 号.
提示出错信息:
syntax error near unexpected token then
同理,还有很多出错信息 比如
syntax error near unexpected token fi 等都是这样引起的.

5.if 后面一定要跟上 then. 同理
elif 后面一定要跟上 then.
不然提示出错信息:
syntax error near unexpected token else

1)if 语句后面需要跟着then,同时前面要有分号;
2) 空格非常重要,shell 会认为空格前的为一个命令,如果a=3 认为是赋值操作,如果写成a = 3,那么就会认为a为一个命令 this=`ls -l |grep ‘^-’ | wc -l `
3) 操作符之间要用空格分开 ,如 test ! -d $1,其中的!和-d就要用空格分开
空格是命令解析中的重要分隔符

6. 命令和其后的参数或对象之间一定要有空格

if [ -x"~/Workspace/shell/a.sh" ];then

只有 -x 后有空格才表示紧跟其后的字符串是否指向一个可执行的文件名,否则就成了测试 -x"~/Workspace/shell/a.sh" 这个字符串是不是空。

7.取变量值的符号'$'和后边的变量或括号不能有空格


http://blog.sina.com.cn/s/blog_5f5716580100l9k1.html

hard quote:单引号,关闭所有的meta(什么是meta,自己查吧)
soft quote:双引号,关闭大部分的meta,但是某些保留,例如$
这里也提一下escape:反斜杠,它只会关闭紧跟escape之后的字符。

参考cu上经典的十三问,hard quote关闭shell的meta,而soft quote关闭大部分的shell meta,但是$例外,对于bash的来说,命令的解释是从左到右的,首先遇到hard quote和soft quote,所作的解释是不一样,看这个例子:
$ a=1
$ echo "$a"
1<wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">#$被bash解释到</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">$ echo "'$a'"</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">'1'</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">#hard quote被关闭</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">$ echo '$a'</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">$a</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">#$被hard quote关闭</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">$ echo '"$a"'</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">"$a"</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">#所有都被关闭了</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">如果能够理解上面的了,那基本知道怎么用单引号和双引号了。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">而对于awk、ed等等命令,需要区分shell meta和command meta</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">对于awk来说,它的{ }:是将其内的命令置于non-named function 中执行,awk用{ }来区分命令段,例如BEGIN,END等等。举个例子:</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">$ awk {print $1} b.txt</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">awk: line 2: missing } near end of file</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">看看报错,如果直接使用{ },那么我们知道{ }没有被shell关闭,也就是说它看成shell的meta了,当然报错了,所以要关闭,同时shell中的$也关闭,也就是$需要成为awk的meta了,很好理解吧?如下输出了内容。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">awk '{print $1}' b.txt</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">./0/137/qq/.zhangqx</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">./0/10/qq/.zyyao</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">./0/115/qq/.ywt</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">既然要要关闭{ },那么也可以用soft quote,</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">awk "{print $0}" b.txt</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">0</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">0</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">0</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">可以看到,soft quote预期的关闭了{ },但是$还是被shell解释了,这次我们可以用escape,将$关闭了。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">$ awk "{print \$0}" b.txt</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">./0/137/qq/.zhangqx</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">./0/10/qq/.zyyao</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">./0/115/qq/.ywt</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">说到这里,可以大家都一点明白了,并不是awk '{ }' urfile是awk的固定语法,而是为了让awk能够跳脱shell的偷取其命令和参数,看看这个例子就明白了。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">$ awk \{print\ \$0\} b.txt</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">./0/137/qq/.zhangqx</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">./0/10/qq/.zyyao</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">所作的一切是为了让awk或者自己的meta,请切记注意print后的空格,也要关闭,要不又被shell解释了。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">2. 双引号 ( " " )</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">双引号作用与单引号类似,区别在于它没有那么严格。单引号告诉shell忽略所有特殊字符,而双引号只要求忽略大多数,具体说,括在双引号中的三种特殊字符不被忽略:$,\,` ,即双引号会解释字符串的特别意思,而单引号直接使用字符串.</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">3、. 反斜杠 backslash-escaped( \ )</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">反斜杠一般用作转义字符,或称逃脱字符,linux如果echo要让转义字符发生作用,就要使用-e选项,且转义字符要使用双引号</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">echo -e "\n"</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(248,236,216)">反斜杠的另一种作用,就是当反斜杠用于一行的最后一个字符时,shell把行尾的反斜杠作为续行,这种结构在分几行输入长命令时经常使用。</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

BTW

4、$()和``的区别
反引号和$()的功能是命令替换,将反引号或$()中的字符串做为命令来执行,我们在用shell编程时经常用的到 将系统命令的执行结果赋给一个变量
但反引号内不能再引用反引号,而$()中可以引用反引号


1. 除了赋值操作=左右两边不能有空格,其他都要有空格

2. 如果在一行写多句,每一句就要用;分隔

3. 字符串变量也可以自动变成整数a='123'; echo `expr $a + 1`


分享到:
评论

相关推荐

    shell编程学习笔记

    在一串命令中,还需要藉由其他的命令提供的信息,可以使用反单引号『`命令`』或 『$(命令)』 特别注意:那个 ` 是键盘上方的数字键 1 左边那个按键,而不是单引号! 例如想要取得核心版本的配置:『version=$(uname ...

    shell脚本编程基础(四)

    使用转义字符(反斜线)来将单引号转义; 在第一个有问题的地方添加了反斜线字符来转义don’t中的单引号。在第二个有问题的地方将this’ll用双引号圈起来。两种方法都能正常辨别出这个值。 for循环假定每个值都是用...

    学习笔记---《shell从入门到精髓》张春晓

    反引号(``)键盘1左边,作为shell命令执行。()也有这个作用【相当于在此处插入新的shell,此处会被新的shell的echo内容全部替换】 \(转义字符)---特殊字符转为普通字符 5,默认情况下,所有的变量都是全局变量!...

    vim编辑器中 熟练掌握 shell环境变量

    熟练掌握 shell环境变量 shell就是用户与操作系统内核之间的接口,起着协调用户与系统的一致性和在用户与系统之间进行交互的作用。...双引号内允许变量替换,而单引号内则不可以。 2. shell变量的作用域 3.环境变量

    shell-programming-study-notes:shell编程学习笔记

    常见的有美元符号 $ 反斜线 \ 和单引号 '' 与双引号 "" 由双引号括起来的字符,除$,倒引号 ` 和反斜线 \ 仍保留其特殊功能外,其余字符均作为普通字符对待 由倒引号括起来的字符串被shell解释为命令行,在执行时,...

    node-php-escape-shell:基于php shell元字符的转义函数

    在 Windows 上, php_escapeshellarg() 改为删除百分号,用空格替换双引号并在字符串周围添加双引号。 php_escapeshellarg ( cmd ) escapeshellarg() 为 php 原始的 escapeshellarg 函数添加了更多选项,is...

    shell脚本命令行参数简介

    之所以用到命令行参数,关键在于shell脚本需要与运行脚本的人员进行交互。...同时输入多个参数可以是数值也可以是字符串)时,必须使用空格分隔要想在参数值中包含空格,就必须使用单引号或双引号)当参数多于9个后,

    Shell编程之Shell变量学习总结

    双引号内的特殊字符如$等,保持原本特性 代码如下:[root@bogon ~]# var=”lang is $LANG”[root@bogon ~]# echo %varroot@bogon ~]# echo $varlang is zh_CN.UTF-81. 单引号内的特殊字符则仅为一般字符 代码如下:...

    PowerShell命令中包含空格如何运行?

    在PowerShell中,可以直接运行命令。...均可以使用如下方式运行程序: &lt;程序文件名&gt;.&lt;...双引号、单引号均可。比如C:\Program Files\Winrar\rar.exe,这个是Winrar自带的一个命令行工具,可以用于

    Shell 字符串拼接的实现示例

    # 不过后面的例子, 通常是用 双引号, 具体原因, 以后会解释\nstr02='str02' echo ${str02} # 对引号的转义, 这里就不提了 2. 简单拼接 概述 简单的字符串拼接 脚本 1: #/bin/bash # 字面量拼接 # 1. 两个串依次...

    shell脚本第二天——变量

    变量 变量名定义:字母,数字,下划线 ...当定义值里面包含空格的时候,需要给值加双引号或者单引号 name=zhang san 作为规范最好加上双引号。而且,再给变量赋值后,会自动转换成字符型。 在这里讲

    3000字扫盲shell基础知识(新手必备)

    不论前端还是后端,再学会Linux的基础上,还应该学会写一些简单的Shell。不止是工作,日常中想做... 字符串的变量可以用单引号或者双引号括起来。 定义变量: ➜ ~ name=xiaoka ➜ ~ age=2 无效的定义 [ root@iz2ze

    rshell:CS 100 外壳

    壳 CS 100 外壳 构建和运行 make ./bin/rshell 特征 在单个条目中支持多达 1000... 支持单引号和双引号 kkita004@hammer.cs.ucr.edu$ echo " This sentence will be output. " This sentence will be output. kkita004@

    计算机病毒与防护:Linux基本使用.pptx

    跳脱字符“\”,将特殊字符转换成普通字符(“\”也可以是单或双引号),如:空格、@、$等 空格 cd my\ documents cd ‘my documents’ @ cd my\@documents cd ‘my@documents’ 基本使用 登录 密码无回显 超级用户...

    WinCE脚本解析程序(写写文本就能做很多意想不到的事情)

    (5)具有合理的严格性和纠错处理,每一行中在'='后边非关键字处可随意添加空格、制表符、逗号,不影响正常解析,关键内容可以加上引号(单引号和双引号的区别,将在后述提到),如包含空格的路径、参数等。...

    cl-shellwords:通用Lisp库,用于转义外壳程序的字符串,或根据外壳程序规则拆分字符串

    cl-shellwords CL-shellwords是Ruby的shellwords.rb到Common Lisp的移植。... 空格通常用作单词分隔符,除非在前面加上反斜杠或将其括在单引号或双引号中。 Examples:(split " example string " ); =&gt; ("example" "str

    CETool(最新2013.8)

    (5)具有合理的严格性和纠错处理,每一行中在'='后边非关键字处可随意添加空格、制表符、逗号,不影响正常解析,关键内容可以加上引号(单引号和双引号的区别,将在后述提到),如包含空格的路径、参数等。...

    入门学习Linux常用必会60个命令实例详解doc/txt

    从图1中可以看到,笔者系统中可以使用的Shell有bash(缺省)、csh、sh、tcsh四种。 exit 1.作用 exit命令的作用是退出系统,它的使用权限是所有用户。 2.格式 exit 3.参数 exit命令没有参数,运行后退出系统...

    rar压缩软件.rar

    在 Unix 中,你需要将 RAR 命令行参数置于单引号或双引号中,防止被 Shell 解释。例如,这个命令将从当前目录中所有的 RAR 压缩文件中解压的 *.asm 文件: rar e '*.rar' '*.asm' 命令可以是下列中的任何一个:...

Global site tag (gtag.js) - Google Analytics