Hi all,
I'm relatively new to bourne shell scripting, and I'm trying to write a planner script.
#! /bin/sh
# Option 2
# Get date from user
echo "Please enter day, month and year of event:"
read date
if [ $# -eq 3 ]
then
if [ $3 -lt date '+%Y' ]
then
echo Invalid year entered.
else
if [ $2 -le `date '+%m'`]
then
echo Invalid date entered.
else
if [ $1 -le `date '+%d'` ]
then
echo Invalid month entered.
else
# Get hour, minute and length from user
echo "Please enter hour, minute and length of event:"
read time
# Check that hour is valid.
if [ $1 -lt 0 -a -gt 23 ]
then
echo Invalid hour entered. Must be between 0 and 23 inclusive.
else
if [ $2 -lt 0 -o -gt 59 ]
then
echo Invalid minute entered. Must be between 0 and 59 inclusive.
else
# Get title from user
echo "Please enter a title for your event:"
read title
fi
fi
fi
fi
fi
else
echo Invalid number of arguments entered.
fi
- #! /bin/sh
- # Option 2
- # Get date from user
- echo "Please enter day, month and year of event:"
- read date
- if [ $# -eq 3 ]
- then
- if [ $3 -lt date '+%Y' ]
- then
- echo Invalid year entered.
- else
- if [ $2 -le `date '+%m'`]
- then
- echo Invalid date entered.
- else
- if [ $1 -le `date '+%d'` ]
- then
- echo Invalid month entered.
- else
- # Get hour, minute and length from user
- echo "Please enter hour, minute and length of event:"
- read time
- # Check that hour is valid.
- if [ $1 -lt 0 -a -gt 23 ]
- then
- echo Invalid hour entered. Must be between 0 and 23 inclusive.
- else
- if [ $2 -lt 0 -o -gt 59 ]
- then
- echo Invalid minute entered. Must be between 0 and 59 inclusive.
- else
- # Get title from user
- echo "Please enter a title for your event:"
- read title
- fi
- fi
- fi
- fi
- fi
- else
- echo Invalid number of arguments entered.
- fi
Thats what I've got so far, which is to do with adding a entry to the planner datafile (planner.txt). I'm stuck with the bit about checking that the user entered the right number of arguments. I've gathered that $# can't be used to check for the number of arguments entered via read like they can straight to the script. Can someone point me in the right direction with this?
Would also appreciate if someone could suggest any ways I could write what I've got in a better way. Seems a bit long and I'm sure there must be a better way.
Hopefully someone can help.
