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

原创 2013年12月04日 11:18:49

不加-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 "<>"

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。在执行命令。

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”.


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.



JS window eval and load script!

var globalEval = function globalEval(src) { if (window.execScript) { window.execScript(s...

phantomjs and casperjs - 安装

你能看到这篇文章,说明你想学习 phantomjs 和 casperjs 这两个东西,所以我在这不想多说什么废话。下面是这两个工具的官方网址,接下来我会介绍他们两个的如何安装。 phantomj...

FunDA(17)- 示范:异常处理与事后处理 - Exceptions handling and Finalizers

作为一个能安全运行的工具库,为了保证占用资源的安全性,对异常处理(exception handling)和事后处理(final clean-up)的支持是不可或缺的。FunDA的数据流FDAPipeL...

HDU 5410 CRB and His Birthday(01+完全)

CRB and His BirthdayTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe...

hdu 1029 Ignatius and the Princess IV(主元素问题):

Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ...

hdu5627 Clarke and MST (并查集)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submissio...

Advanced Graphics and Animations for iOS Apps(session 419)

学习与延伸 这篇文章主要是学习完Advanced Graphics and Animations for iOS Apps这个session后的总结和相应细节的延伸和细化。主要内容为图形性能与测...

【Android API】Tasks and Back Stack

Back Stack 1.当前activity启动另一个新的activity时,新的activity进入栈顶,之前的activity状态变为stopped,但是它的user interface被...

【分块】 HDOJ 5286 wyh2000 and sequence

不错的分块题。。。附上BC的官方题解。。。 令f(l,r)表示[l,r]的答案。 我们对于序列分块,对于第i块,令Si为第i块的左端点。 令g(a,b)表示第a块开头到第b块末尾这一段序列的答案。下...

codeforces #312C 558C C. Amr and Chemistry(位运算)

题目链接: 点击打开链接 题目大意: 给出一些数,他们能做的操作是左移和右移,问最少做多少次操作能把他们都变成相同的数 题目分析: 首先要知道,左移的话的只是在二进制数末尾填0,而右移则...