Re: Help with very basic bourne shell



On 2006-06-14, Jack Arnst wrote:
Greetings. I am having a very frustrating time figuring out why my very
simple script does not work in bourne shell. Here it is. Your help is
greatly appreciated.

#!/bin/sh
set -- `date "+%y %m %d"`
let year=$1
let month=$2
let day=$3

There is no let command in the Bourne shell; some Bourne-type
shells have it, but it is non-standard. Use:

year=$1
month=$2
day=$3

Another way to it is:

eval "$(date "$@" "+
year=%Y
month=%m
day=%d"
)"

year=${year#0}
month=${month#0}
year=`expr 2000+$year`

You don't need that if you use the more accurate %Y in your date
format; and you don;t need expr in most Bourne-type shells:

y=$(( $year + 2000 ))

if [$month -eq 6]; then

You need spaces around [ and ]:

if [ $month -eq 6 ]; then

echo $month
else
echo "Not June"
echo $year
fi
exit 1


Output:
./test2.sh: line 9: [6: command not found
Not June
2000+6

What I want is to check to see if it is the end of the month so I can run
some commands in this script.

You can get the number of days in the month with these functions:

_days_in_month() ## USAGE: days_in_month month [year]
{ ## Result is stored in $_DAYS_IN_MONTH
case ${1#0} in
9|4|6|11) _DAYS_IN_MONTH=30 ;; ## 30 days hath September...
1|3|5|7|8|10|12) _DAYS_IN_MONTH=31 ;;
2) is_leap_year ${2:-`date +%Y`} && _DAYS_IN_MONTH=29 || _DAYS_IN_MONTH=28 ;;
*) _DAYS_IN_MONTH=; return 5 ;;
esac
}

is_leap_year() { ## USAGE: is_leap_year [year]
ily_year=${1:-`date +%Y`}
case $ily_year in
*0[48] |\
*[2468][048] |\
*[13579][26] |\
*[13579][26]0|\
*[2468][048]00 |\
*[13579][26]00 ) return 0 ;;
*) return 1 ;;
esac
}


I would like these commands to run sequentially so I would like some
way to wait until 1 finishes executing before the next command is
called, but I haven't even got passed the date part as you can see.
This script will be called by a cron job so I don't want any output.

Put the commands sequentially in a script and call that from cron.


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.



Relevant Pages

  • rlogin via expect mysteriously aborts when run from cron
    ... I'm not sure whether this is a problem related to shell, ... I have an application which, at some point, executes a zsh script. ... When investigating the output which was sent to me from the cron job ... I had one case where even a command ...
    (comp.lang.tcl)
  • Re: rlogin via expect mysteriously aborts when run from cron
    ... I have an application which, at some point, executes a zsh script. ... When investigating the output which was sent to me from the cron job ... I had one case where even a command ...
    (comp.lang.tcl)
  • Re: Need some small help on shell script - delete old files
    ... I am using your script. ... recent dump is always left un-compressed with no .gz extension. ... or use the "wait" command to see if it must delay the start of the ... The second dump has to be coming from an alternate CRON job or an AT ...
    (comp.unix.shell)
  • Cron entry & .profile question
    ... Cron jobs can be confusing at times. ... When I run a script I wrote from ... The bourne shell uses the command "." ... Should I run my cron job as root or should I make a new cron entry ...
    (SunManagers)
  • Re: Perl scrpit to reschedule AT command?
    ... I once saw a perl script that was able to reschedule the AT command ... up a cron job. ... Also do not wish to use the scheduling facility in Oracle ...
    (comp.lang.perl.misc)