rekobeko
rekobeko

Reputation: 89

execute find command with xargs in bash script

I'm trying to execute this command in in a bash script

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

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

Upvotes: 1

Views: 616

Answers (1)

shellter
shellter

Reputation: 37268

Variables aren't expanded inside of single quotes, i.e. '...$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. 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. If you're sure you'll never work in a Solaris, AIX, or HPUX shop, then use the improved features. Also, if you're using new OS's, then xargs likely supports parallel processing, which I don't think find ... -exec ... \+ will do.

I hope this helps.

Upvotes: 1

Related Questions