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

一 xargs

1 xargs puts stdout as parameters

$  find . -name  "*.tcl" -o -name "*.pl"|wc -l
      38
$  find . -name  "*.tcl" -o -name "*.pl"|xargs wc -l
     207 ./bin/a.tcl
    5180 ./bin/b.tcl
     211 ./lib/c.pl
.......

没有xargs则是统计文件个数

有xargs统计每个文件的行数

相当于

wc -l a.tcl b.tcl c.pl...

用xargs等于将输入a.tcl b.tcl c.pl...作为文件参数,作为输入

2  ls|grep -v '^.*OFC16L41.0.*$'
grep 支持正则表达式
xargs 指向的是文件内容,因为xargs把所有的文件当参数给grep,or rm
等于grep felixzh filename or rm filename(one by one)
xargs 将整体结果分开;
如:ls|grep -v '^.*OFC16L41.0.*$'
结果是
tclOFC15L41.0
LBUOFC13L41.0
buOFC12L41.0
...
不用xargs则,grep felixzh filename(即文件名为tclOFC15L41.0LBUOFC13L41.0buOFC12L41.0...)连在一块的文件
rm filename(即文件名为tclOFC15L41.0LBUOFC13L41.0buOFC12L41.0...)连在一块的文件
使用xargs 则grep felixzh tclOFC15L41 (LBUOFC13L41,buOFC12L41.0,...) one by one
rm tclOFC15L41 (LBUOFC13L41,buOFC12L41.0,...) one by one

3 xargs这个东西,不是一行一行作为输入,而是一项一项的
不加-l 。则仅仅是文件名,可以直接删除 ls -tr|head -3
$ ls -ltr|head -3 |xargs grep total 
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 and $()/``and typeset

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值