UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
  #1  
Old June 3rd, 2004, 09:20 PM
Tajikistanian Tajikistanian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 Tajikistanian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post Bourne Shell Scripting Assistance

Hi all,

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

Code:
#! /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.

Also need some help with sorting the datafile for the planner. The entries are in the format:
16/08/2004 12:17 35 Some event
20/01/2005 13:25 17 some event
20/13/1900 22:00 17 some event
20/16/1925 13:15 25 some event
20/09/1986 13:56 25 some event
and I want to sort them by year, month and then day.

Reply With Quote
  #2  
Old June 16th, 2004, 11:40 PM
Nevets Nevets is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 11 Nevets User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Here are some tips that might help you out, or at least put you in the right direction:

PHP Code:
# Get date from user 
echo "Please enter day, month and year of event (DDMMYY): \c" 
read date 

day
=`echo $date | cut -c1-2`
month=`echo $date | cut -c3-4`
year=`echo $date | cut -c5-6`


if [  
$year -lt `date '+%Y'` -o $month -le `date '+%m'` -o $day -le `date '+%d'` ] ; then
    
echo "Invalid date format"
    
exit 1
else 
    
# Get hour, minute and length from user 
    
echo "Please enter start time of the event (hh:mm)" 
    
read time 
    
# do a similar cut as above
    # and validate as you like
    # if err, exit 1
    
echo "Enter event duration in minutes: \c"
    
read duration    
fi

# Get title from user 
echo "Please enter a title for your event:" 
read title 


I find that it has also helps to wrap prompts/validations in while loops (eg. while not valid answer) so that fat fingered typing by the user doesn't cause them to start over from scratch.

Reply With Quote
  #3  
Old June 16th, 2004, 11:43 PM
Nevets Nevets is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 11 Nevets User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
For your sort problem, do check out the man pages on sort
(man sort)

Some things to look for are how to set the field delimeter, in your case a "/". Then how to sort based on the 3rd, 2nd and 1st fields.

Also check out the -o swicth so you don't have to use a temp file.

Good luck.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Bourne Shell Scripting Assistance


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway