Find EOM in Unix crontab

I have a command to run a script in crontab but it doesn't work.  This is the command   test `echo \`cal\`|awk '{print $NF}'` -eq `date +\%d`.  What is wrong with it?
OCCUAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
Backslashes (\) must be escaped (\\) under cron because it eats up the first one:

test `echo \\`cal\\`|awk '{print $NF}'` -eq `date +\%d` &&  <run the script>

The backslash before % is also consumed by cron, so that the percent sign is passed to "date" instead of being interpreted as start of stdin. It must thus not be escaped!

Better use the "$( ... )" notation instead of backticks. No escaping is required then:

test $(echo $(cal) |awk '{print $NF}') -eq $(date +\%d) && <run the script>

You could also replace "test" with "[ ... ]":

[ $(echo $(cal) |awk '{print $NF}') -eq $(date +\%d) ] && <run the script>

All of the above worked for me under AIX.


wmp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OCCUAuthor Commented:
If it returns a true it will run script.  This script does work.
0
woolmilkporcCommented:
Did you try my suggestions?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

OCCUAuthor Commented:
I will try it tonight and let you know.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
This part:

echo \`cal\`

is not executing the cal command, it is merely echoing the string cal - this is why your command in your script is not working.
0
woolmilkporcCommented:
If you're on Linux (or have GNU date installed on Unix) you could also use its features instead of "cal":

test $(date -d "-1 days +1 month" "+\%d") -eq $(date "+\%d")  && <run the script>
0
OCCUAuthor Commented:
If I remove the echo would it work?
0
woolmilkporcCommented:
Gerwin - please note that it's nested backticks!

So e. g.

echo `echo \`cal\``

works on command line, but not under cron. The inner backticks must be escaped twice there.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Only change you need to make is getting the cal command to run:

test `echo $(cal) | awk '{print $NF}'` -eq `date +%d`

Open in new window

^^^ this is working and is returning a $? of 0 (so true).
0
Gerwin Jansen, EE MVETopic Advisor Commented:
@wmp - I know - I never nest backticks :D
0
woolmilkporcCommented:
>> If I remove the echo would it work?   <<

No, because you'll get multi line output then.

This will work:

cal | tail -1 |awk '{print $NF}'
0
woolmilkporcCommented:
Gerwin - I don't even use backticks if at all possible!
0
OCCUAuthor Commented:
I was able to get my script to run by using [ $(echo $(cal) |awk '{print $NF}') -eq $(date +\%d) ] &&.

Thanks for your help
0
woolmilkporcCommented:
You're always welcome. Glad I could help!

wmp
0
woolmilkporcCommented:
Please, what are your reasons for giving grade B?
You wrote above that you followed exactly my suggestion!

Please read here about awarding grades.

Quote:
B is the grade given for acceptable solutions, or a link to an acceptable solution. A B grade means the solution given lacked some information or required you to do a good amount of extra work to resolve the problem. When closing the question, the asker should explain why a B grade was awarded.
Should you want to review your decision click "Request Attention" above and explain your demand.

wmp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office Productivity

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.