Solved

Find EOM in Unix crontab

Posted on 2014-10-31
15
296 Views
Last Modified: 2014-11-07
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?
0
Comment
Question by:OCCU
  • 8
  • 4
  • 3
15 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
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
 

Author Comment

by:OCCU
Comment Utility
If it returns a true it will run script.  This script does work.
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
Did you try my suggestions?
0
 

Author Comment

by:OCCU
Comment Utility
I will try it tonight and let you know.
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
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
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
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
 

Author Comment

by:OCCU
Comment Utility
If I remove the echo would it work?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
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
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
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
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
@wmp - I know - I never nest backticks :D
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
>> 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
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
Gerwin - I don't even use backticks if at all possible!
0
 

Author Comment

by:OCCU
Comment Utility
I was able to get my script to run by using [ $(echo $(cal) |awk '{print $NF}') -eq $(date +\%d) ] &&.

Thanks for your help
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
You're always welcome. Glad I could help!

wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
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

Featured Post

Shouldn't all users have the same email signature?

You wouldn't let your users design their own business cards, would you? So, why do you let them design their own email signatures? Think of the damage they could be doing to your brand reputation! Choose the easy way to manage set up and add email signatures for all users.

Join & Write a Comment

I previously wrote an article addressing the use of UBCD4WIN and SARDU. All are great, but I have always been an advocate of SARDU. Recently it was suggested that I go back and take a look at Easy2Boot in comparison.
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Viewers will learn how to use the Hootsuite Dashboard.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now