Bourne Shell Scripting Assistance

  • Tajikistanian
  • Born
  • Born
  • Tajikistanian
  • Posts: 1

Post 3+ Months Ago

Hi all,

I'm relatively new to bourne shell scripting, and I'm trying to write a planner script.

Code: [ Select ]
#! /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
  1. #! /bin/sh
  2. # Option 2
  3. # Get date from user
  4. echo "Please enter day, month and year of event:"
  5. read date
  6. if [ $# -eq 3 ]
  7. then
  8.     if [ $3 -lt date '+%Y' ]
  9.     then
  10.         echo Invalid year entered.
  11.     else
  12.         if [ $2 -le `date '+%m'`]
  13.         then
  14.             echo Invalid date entered.
  15.         else
  16.             if [ $1 -le `date '+%d'` ]
  17.             then
  18.                 echo Invalid month entered.
  19.             else
  20.                 # Get hour, minute and length from user
  21.                 echo "Please enter hour, minute and length of event:"
  22.                 read time
  23.                 # Check that hour is valid.
  24.                 if [ $1 -lt 0 -a -gt 23 ]
  25.                 then
  26.                     echo Invalid hour entered. Must be between 0 and 23 inclusive.
  27.                 else
  28.                     if [ $2 -lt 0 -o -gt 59 ]
  29.                     then
  30.                         echo Invalid minute entered. Must be between 0 and 59 inclusive.
  31.                     else
  32.                         # Get title from user
  33.                         echo "Please enter a title for your event:"
  34.                         read title
  35.                     fi
  36.                 fi
  37.             fi
  38.         fi
  39.     fi
  40. else
  41.     echo Invalid number of arguments entered.
  42. 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. :?
  • dr_geek
  • Beginner
  • Beginner
  • User avatar
  • Posts: 62
  • Loc: london town

Post 3+ Months Ago

`echo $date | wc -w`

will yield the number of words ine $date

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 103 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.