In article
<
c5184d4a-686f-4ee8...@f30g2000yqh.googlegroups.com>,
HB <
herbert....@gmail.com> wrote:
> Hi All,
>
> I apologize up front as I posted this request in the BashScripts.org
> group thinking it was this group (this group has much more
> participation).
>
> I have a simple shell script to run a check on the 2nd Tuesday of
> every month which I thought worked pretty well. However, it appears
> that it did not work for this given month and I'd like to see if
> anyone has any suggestions as to how it can be fixed. Here is the
> code:
>
>
> today=`/bin/date +%d | cut -d"0" -f2`
> tue=`cal | awk {'print $3'} | xargs | /usr/bin/cut -d" " -f3`
>
>
> if [ "$today" -ne "$tue" ] ; then
Why do you need this check? If this is a cron job, why don't you
schedule it to ONLY run on Tuesdays?
>
>
> exit 1
>
>
> else
>
>
> run check here...
>
>
> fi
>
>
> exit 0
>
>
> So, the line:
>
>
> cal | awk {'print $3'} | xargs | /usr/bin/cut -d" " -f3
>
>
> is not working as the awk pipe skips the blank lines:
>
>
> cal | awk {'print $3'}
>
>
> Tu
> 3
> 7
> 14
> 21
> 28
>
>
> which puts '3' into the awk line and causes '7' to be the 2nd Tuesday
> which it is not.
Look at the full output of cal:
February 2012
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29
Notice that the first week doesn't contain a Tuesday, but you're
printing $3 anyway. If you want to skip the first week if it doesn't
have a Tuesday, try:
awk 'NF > 4 {print $3}'
In fact, you don't need xargs or cut, either. This will do everything
in awk:
cal | awk '/^ *[1-9]/ && NF > 4 {print $3; exit}'
But there's really no need to use cal or awk at all. The second Tuesday
of the month will always be from 7 to 13. So just check:
today=`/bin/date +%d`
if [ "$today" -ge 7 ] && [ "$today" -le 13 ]; then
--
Barry Margolin,
bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***