Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Script help...

37 views
Skip to first unread message

HB

unread,
Feb 13, 2012, 4:21:08 PM2/13/12
to
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


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.


I hope I have not confused you all too much. Is there anyway to fix
this issue to allow it to work as intended?


Thanks in advance,


Herb

Barry Margolin

unread,
Feb 13, 2012, 6:57:42 PM2/13/12
to
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 ***

DennisW

unread,
Feb 13, 2012, 7:23:41 PM2/13/12
to
read -r dow dom < <(date '+%w %e')
if ((dow == 2 && dom >= 11 && dom <= 17))
then
echo "It's the second Tuesday."
fi

11 and 17 assume you mean the second Tuesday rather that the Tuesday
in the second week.

Barry Margolin

unread,
Feb 13, 2012, 7:34:45 PM2/13/12
to
In article
<2ce21a67-ac91-47f9...@m24g2000yqb.googlegroups.com>,
DennisW <dennistw...@gmail.com> wrote:

> read -r dow dom < <(date '+%w %e')
> if ((dow == 2 && dom >= 11 && dom <= 17))
> then
> echo "It's the second Tuesday."
> fi

You shouldn't need to get the dow. I assume this is being done in a
cron job, and you can simply schedule it to run every Tuesday.

> 11 and 17 assume you mean the second Tuesday rather that the Tuesday
> in the second week.

Where did you get 11 and 17 from? If the month starts on a Tuesday, the
second Tuesday is the 8th.

I also got this wrong in my earlier response, but I was only off by 1.
The range should be 8 to 14.

DennisW

unread,
Feb 13, 2012, 11:33:54 PM2/13/12
to
On Feb 13, 6:34 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article
> <2ce21a67-ac91-47f9-97ce-176aa96f0...@m24g2000yqb.googlegroups.com>,
>
>  DennisW <dennistwilliam...@gmail.com> wrote:
> > read -r dow dom < <(date '+%w %e')
> > if ((dow == 2 && dom >= 11 && dom <= 17))
> > then
> >     echo "It's the second Tuesday."
> > fi
>
> You shouldn't need to get the dow.  I assume this is being done in a
> cron job, and you can simply schedule it to run every Tuesday.
>
> > 11 and 17 assume you mean the second Tuesday rather that the Tuesday
> > in the second week.
>
> Where did you get 11 and 17 from?  If the month starts on a Tuesday, the
> second Tuesday is the 8th.
>
> I also got this wrong in my earlier response, but I was only off by 1.
> The range should be 8 to 14.
>
> --
> Barry Margolin, bar...@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***

I was incorrect - 8-14 is correct. The dow would be useful if the
script were to be run every day and certain things were to be done on
selected days (or also if it was important to enforce adherence to a
particular dow without relying on the cron to be correct).

Chris F.A. Johnson

unread,
Feb 14, 2012, 2:30:56 PM2/14/12
to
> read -r dow dom < <(date '+%w %e')
> if ((dow == 2 && dom >= 11 && dom <= 17))
> then
> echo "It's the second Tuesday."
> fi
>
> 11 and 17 assume you mean the second Tuesday rather that the Tuesday
> in the second week.

For those not using bash (and even those who are):

eval "date '+dow=%w dom=%e'"
if [ "$dow" -eq 2 ] && [ "$dom" -ge 8 ] && [ "$dom" -le 14 ]
then
: whatever
done


--
Chris F.A. Johnson <http://cfajohnson.com>
Author: =======================
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)

Ralf Goertz

unread,
Feb 16, 2012, 2:37:12 AM2/16/12
to
Chris F.A. Johnson wrote:

> For those not using bash (and even those who are):
>
> eval "date '+dow=%w dom=%e'"
> if [ "$dow" -eq 2 ] && [ "$dom" -ge 8 ] && [ "$dom" -le 14 ]
> then
> : whatever
> done

Hm, apart from the obvious "done" "fi" mixup this doesn't work for me as
the eval line doesn't seem to get evaluated, it only gets printed. $dow
and $dom are empty in line 2.

DennisW

unread,
Feb 16, 2012, 7:04:33 AM2/16/12
to
The double quotes in the eval line should be $() or `` (backticks).

eval $(date '+dow=%w dom=%e')

eval `date '+dow=%w dom=%e'`

Chris F.A. Johnson

unread,
Feb 16, 2012, 5:08:23 PM2/16/12
to
I'll blame it on lack of sleep...

Chris F.A. Johnson

unread,
Feb 16, 2012, 5:21:02 PM2/16/12
to
On 2012-02-16, Chris F.A. Johnson wrote:
> On 2012-02-16, Ralf Goertz wrote:
>> Chris F.A. Johnson wrote:
>>
>>> For those not using bash (and even those who are):
>>>
>>> eval "date '+dow=%w dom=%e'"
>>> if [ "$dow" -eq 2 ] && [ "$dom" -ge 8 ] && [ "$dom" -le 14 ]
>>> then
>>> : whatever
>>> done
>>
>> Hm, apart from the obvious "done" "fi" mixup this doesn't work for me as
>> the eval line doesn't seem to get evaluated, it only gets printed. $dow
>> and $dom are empty in line 2.
>
> I'll blame it on lack of sleep...

...and I don't know what happened to the corrected script:

eval "$(date '+dow=%w dom=%e')"
if [ "$dow" -eq 2 ] && [ "$dom" -ge 8 ] && [ "$dom" -le 14 ]
then
: whatever
fi

HB

unread,
Feb 17, 2012, 1:51:17 PM2/17/12
to
Thanks to all who replied, it was very helpful.

Since I only need to run my check on the 2nd Tuesday of every month, I
went with Barry's suggestion to run my cron only on Tuesdays as:

today=`/bin/date +%d`

if [ "$today" -ge 8 ]&&[ "$today" -le 14 ]; then

run check...
exit 0

else

exit 1

fi

Thanks again,

HB
0 new messages