Chinaunix首页 | 论坛 | 博客
  • 博客访问: 92580
  • 博文数量: 17
  • 博客积分: 691
  • 博客等级: 上士
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-27 08:50
文章分类
文章存档

2012年(1)

2011年(16)

分类: 嵌入式

2011-10-26 15:28:52

``用在产生的结果不会再送给shell解释,而只作为赋值时直接使用,文本输出时与""配合使用;
()有在产生的结果还会再作进一步解释时,用与不用都可以,属于"脱裤子放屁"类型的,故除了提高程序可读性之外,一般不使用.

在进行shell编程时,大家是否常会用到替换命令``与()呢?
答案当然是一定的哟~(以上全当屁话^&^)

以前在学习shell命令时,看到替换命令有两个: `` (), 当时做了一些练习,居然都没发现其中的不同点.在以后的编程中,基本都是在用``,也没出现过什么问题(现在想起来,可真是命好啊!). 可今天在编一个备份程序时,突然老是说命令未找到:"command not found",经过翻山越岭,风吹雨打,历尽艰辛,排除万难......,终于发现了``与()之间的区别:

虽然`CMD`和(CMD)都是提取CMD的结果,并把它返回到CMD的引用之处.但它们在运行时采用的机制不同,从而会在不同的条件下出现预想不到的结果.

1. `CMD`在执行时,shell会不管``内是什么东东都先进行解释,再把解释后的最终结果送给shell去执行.如果解释后的最终结果不是shell可执行的命令时,则会出错.当然,仅仅为了把CMD执行后的内容作为文本输出,则没什么问题了啦.
如:
[root@localhost root]# i=0
[root@localhost root]# name=pwd
[root@localhost root]# `$i` # $i已经得到了结果0,再把0送给shell执行,当然出错啦
-bash: 0: command not found
[root@localhost root]# `$name` # $name就已经相当于执行了pwd,得到/root,此时再把/root送给shell执行,当然不可解释啦
-bash: /root: is a directory
[root@localhost root]# echo "`$name`" # 把$name执行后的内容作为文本输出
/root
[root@localhost root]# `pwd`
-bash: /root: is a directory


2. (CMD)在执行时,如果CMD是命令,则直接丢给shell去执行;如果是变量取值,则也仅作第一层的字面解释后丢给shell去执行.
如:
[root@localhost root]# i=0
[root@localhost root]# name=pwd
[root@localhost root]# ($i) # 解释后得到0,再把0送给shell,当然出错啦.
-bash: 0: command not found
[root@localhost root]# ($name) # 解释后得到pwd,再把pwd送给shell执行.
/root
[root@localhost root]# (pwd) # 直接把pwd丢给shell去执行
/root


搞明白了上面的区别,在使用时就好分场合了:
``用在产生的结果不会再送给shell解释,而只作为赋值时直接使用,文本输出时与""配合使用;
()有在产生的结果还会再作进一步解释时,用与不用都可以,属于"脱裤子放屁"类型的,故除了提高程序可读性之外,一般不使用.



特写验证程序如下:

#!/bin/bash
############################################
#
#Purpose: verify the difference of `` and (), those are replace command
#Auhor : Edwin Y.X. Zeng
#e-Mail : zyangxue@163.com
#Time : 2005.10.15 1:40
#Version: 0.0.1
#
############################################

#
#Define the array variables for test
#
declare -a a=(this is a array for test!)
echo ${a[*]}


#
#Nothing used
#
echo -e " Nothing used :"
i=0
while [ "${a[$i]}" != '' ]
do
echo -n "${a[$i]} "
i=`expr $i + 1` #``作为赋值时直接使用
done
echo


#
#Verify the ()
#
echo -e " Verify the () :"
i=0
while [ "${a[($i)]}" != '' ] # ()在此是脱裤子放屁
do
echo -n "${a[$i]} "
i=`expr $i + 1`
done
echo


#
#Verify the ``
#Caution: you will see many error message, please press ctrl-c to break
#
echo -e " Verify the `` :"
i=0
while [ "${a[`$i`]}" != '' ]
do
echo -n "${a[`$i`]} "
i=`expr $i + 1`
done
echo


执行结果:

this is a array for test!

Nothing used :
this is a array for test!

Verify the () :
this is a array for test!

Verify the `` :
./difference: line 1: 0: command not found
./difference: line 1: 0: command not found
this ./difference: line 1: 1: command not found
./difference: line 1: 1: command not found
this ./difference: line 1: 2: command not found
./difference: line 1: 2: command not found
this ./difference: line 1: 3: command not found
./difference: line 1: 3: command not found

阅读(1752) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~