Re: Help with very basic bourne shell
- From: "Chris F.A. Johnson" <cfajohnson@xxxxxxxxx>
- Date: Wed, 14 Jun 2006 04:27:14 -0400
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
.
- Follow-Ups:
- Re: Help with very basic bourne shell
- From: laura fairhead
- Re: Help with very basic bourne shell
- References:
- Help with very basic bourne shell
- From: Jack Arnst
- Help with very basic bourne shell
- Prev by Date: Re: C shell text substitution
- Next by Date: Re: How to print out very three lines in a text file
- Previous by thread: Re: Help with very basic bourne shell
- Next by thread: Re: Help with very basic bourne shell
- Index(es):
Relevant Pages
|