1

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

1
  • You don't need xargs at all. Check out the -exec argument for find Commented Jun 6, 2012 at 11:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thats was the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.