简体   繁体   English

在bash脚本中使用xargs执行find命令

[英]execute find command with xargs in bash script

I'm trying to execute this command in in a bash script 我正在尝试在bash脚本中执行此命令

find /var/log/apache2/access*.gz -type f -newer ./tmpoldfile ! -newer ./tmpnewfile | xargs zcat | grep -E '$MONTH\/$YEAR.*GET.*ad=$ADVERTISER HTTP\/1' -c

If i execute it directly or assign it to a variable it returns 0 如果我直接执行它或将其分配给变量,则返回0

But if i do a echo of the command (with the variables replaced by the script) and execute it in the command line it works. 但是,如果我执行命令的回显(用脚本替换变量)并在命令行中执行它,那么它将起作用。

echo "find /var/log/apache2/access*.gz -type f -newer ./tmpoldfile ! -newer ./tmpnewfile | xargs zcat | grep -E '$MONTH\/$YEAR.*GET.*ad=$ADVERTISER HTTP\/1' -c"

How shold i code it for work 我如何为工作编码

Variables aren't expanded inside of single quotes, ie '...$NOT_EXPANDED ...'. 变量不会在单引号内展开,即'... $ NOT_EXPANDED ...'。 Try 尝试

find /var/log/apache2/access*.gz -type f -newer ./tmpoldfile ! -newer ./tmpnewfile \
| xargs zcat | grep -E "$MONTH\/$YEAR.*GET.*ad=$ADVERTISER HTTP\/1" -c
# newstuff ------------^------------------------------------------^-----------

Same for "or assign it to a variable " 与“或将其分配给变量”相同

varCount=$(find /var/log/apache2/access*.gz -type f -newer ./tmpoldfile ! -newer ./tmpnewfile \
| xargs zcat | grep -E "$MONTH\/$YEAR.*GET.*ad=$ADVERTISER HTTP\/1" -c )

Also, I must comment on -exec . 另外,我必须对-exec评论。 Many finds now support find ... -exec ... \\+ which is (I assume) the equivalent of xargs , but not all OS's have GNU find as their first tool. 现在,许多发现都支持find ... -exec ... \\+ (我假设)等同于xargs ,但并非所有操作系统都将GNU find作为其第一个工具。 If you're sure you'll never work in a Solaris, AIX, or HPUX shop, then use the improved features. 如果您确定永远不会在Solaris,AIX或HPUX商店中工作,请使用经过改进的功能。 Also, if you're using new OS's, then xargs likely supports parallel processing, which I don't think find ... -exec ... \\+ will do. 另外,如果您使用的是新操作系统,则xargs可能支持并行处理,我认为find ... -exec ... \\+不会。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM