关闭

eval and $()/``and typeset and xargs

502人阅读 评论(0) 收藏 举报
分类:

xargs这个东西,不是一行一行作为输入,而是一项一项的
不加-l 。则仅仅是文件名,可以直接删除 ls -tr|head -3
$ ls -ltr|head -3 |xargs grep total --------------------------the same with above rm
grep: can't open total
grep: can't open 907848
grep: can't open drwxrwsr-x


 在非email*.txt 的文件中,查找<>
在除了email*.txt 的文件中,查找<>。
ls |grep -v email |xargs grep "<>"


eval和$(),``区别
a="ls |more"
$ ret=$(a)
ksh: a:  not found
$ ret=`$a`
|more: No such file or directory
$ ret=`ls|more`
$ echo $ret
eval $a-----既不仅替代变量而且替换完执行命令。将a用ls|more替换,又执行了ls|more的命令。而$(a)错误,因为$(),和``只执行命令,不能替换,展开string。在执行命令。
result:
extlab.sh
......


Normally ksh cannot handle a variable name that included an embedded variable:

Mypart=1   # OK, no problem because the variable name is fixed as “Mypart”.

var=”part”

My${var}=1  # ksh error, it doesn‘t know how to handle My${var}.

My usual solution to this problem is to use ‘eval’ which tells ksh to expand the line before executing it:

eval My${var}=1   # OK, ${var} gets expanded first, so ksh executes “Mypart=1”.

Someone on the China COOL team discovered that typeset will also cause the variable to be expanded first.  So instead of using “eval” you could do:

typeset My${var}=1  # OK just like eval


Because eval or typeset will expand the line first, you have to be careful if you don’t want \$ to be expanded too soon.  For example:

print “variable=$My${var}”   # no good, ksh prints the value of variable $My followed by variable ${var} instead of printing the value of $Mypart.

eval print “variable=$My${var}”  # no good, ksh expands $My (blank most likely) before printing

eval print “variable=\$My${var}”  # correct, the eval does not expand the first $ because it has a backslash.  The expanded command is ‘print variable=$Mypart’.  Note that the double quotes were lost too.  If the print was formatted with extra white space, you would need to backslash the double-quotes too.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:103051次
    • 积分:2674
    • 等级:
    • 排名:第13543名
    • 原创:145篇
    • 转载:199篇
    • 译文:0篇
    • 评论:5条