OCCU
asked on
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did you try my suggestions?
ASKER
I will try it tonight and let you know.
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.
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.
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>
test $(date -d "-1 days +1 month" "+\%d") -eq $(date "+\%d") && <run the script>
ASKER
If I remove the echo would it work?
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.
So e. g.
echo `echo \`cal\``
works on command line, but not under cron. The inner backticks must be escaped twice there.
Only change you need to make is getting the cal command to run:
test `echo $(cal) | awk '{print $NF}'` -eq `date +%d`
^^^ this is working and is returning a $? of 0 (so true).
@wmp - I know - I never nest backticks :D
>> 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}'
No, because you'll get multi line output then.
This will work:
cal | tail -1 |awk '{print $NF}'
Gerwin - I don't even use backticks if at all possible!
ASKER
I was able to get my script to run by using [ $(echo $(cal) |awk '{print $NF}') -eq $(date +\%d) ] &&.
Thanks for your help
Thanks for your help
You're always welcome. Glad I could help!
wmp
wmp
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:
wmp
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
ASKER