Computing.Net > Forums > Unix > Scripting problem

Scripting problem

Reply to Message Icon

Original Message
Name: D James
Date: May 21, 2005 at 05:20:09 Pacific
Subject: Scripting problem
OS: Windows XP Pro SP2
CPU/Ram: P4 3.2 / 1 GB
Comment:

I am currently completing a shell script for an assignment in my intro to operating systems class. I have to write a Bourne shell script that allows the user to choose from 2 menu options:
1. Print the users enviromental variables
2. Add to the reminder file
3. Display the contents of the reminder file
4. Display the reminder of the day
5. Exit the Program

The script also has to handle 2 arguments, the first being the name of the file in which the reminders are to be stored and the second argument is the name of the directory which the reminder file is to be placed


I have completed the menu, option 1, option 5 and validated the command line arguments.
The problem I'm having is with option 2, i'm not quite sure how to go about it.

The assignment says that the user should be prompted for the day and description of the new reminder. The input needs to be validated.
•If there is no reminder for the given day in the file the implementation of this command should create a new reminder that takes place on the day, and saved in the reminder file with the format of: day of the week: reminder, eg: Monday: Plan the week
•If there is a reminder for the given day in the file the implementation of this command should ask the user whether to replace the existing one or add to it.
oChallenge: If the response is to replace, the new reminder should replace the existing one for the day.
o If the response is to add, the new reminder needs to be appended to the existing one for the day.

The command should notify the user if successful.

I'm just not sure how to go about so I would be very grateful to anyone who points me in the right direction.

Below is the code I have writen so far (I have excluded the argument validation as it is hard to follow):
--------------------

#!/bin/sh

option=0
notDone=0

while [ $notDone -eq 0 ]
do
echo Please Enter a menu option
echo
echo 1. Print the user enviroment variables
echo 2. Add to the reminder file
echo 3. Display the contents of the reminder file
echo 4. Display the reminder for the day
echo 5. Exit the program

read option
case $option in
1 )
echo
date '+%d/%m/%y'
echo
grep 2553878 /etc/passwd
echo
echo Enviromental Variables:
echo "Home directory: $HOME"
echo "Mail directory: $MAIL"
echo "Path: $PATH"
echo "Login shell: $SHELL"
echo
;;
2 )

;;
3 )

;;
4 )

;;
5 )
notDone=1
;;
* )
echo "Please enter a valid option number"
;;

esac

done
--------------------

Thanx in advanced



Report Offensive Message For Removal

Response Number 1
Name: D James
Date: May 21, 2005 at 05:23:53 Pacific
Subject: Scripting problem
Reply: (edit)

I said choose from 2 menu option I ment say 5, sorry.


Report Offensive Follow Up For Removal

Response Number 2
Name: Luke Chi
Date: May 21, 2005 at 08:44:27 Pacific
Subject: Scripting problem
Reply: (edit)

The most difficult part is to validate the date:

1. with Oracle available:

sqlplus /nolog <<EOF > /dev/null
CONNECT $ORAUID/$ORAPWD
WHENEVER SQLERROR EXIT 1
SELECT TO_DATE('$DATE_INPUT', '$DATE_FORMAT') FROM DUAL;
EOF

if [ $? -eq 1 ]; then
echo "Error: Wrong Date Format - $DATE_INPUT"
exit
fi

2. without Oracle:

http://www.intuitive.com/wicked/showscript.cgi?007-valid-date.sh


Luke Chi


Report Offensive Follow Up For Removal

Response Number 3
Name: D James
Date: May 21, 2005 at 20:12:18 Pacific
Subject: Scripting problem
Reply: (edit)

Thanks for the responce Luke.
I was more wondering how to go about creating a file with a list of days and a reminder for that day eg Moday: Plan the week. And more so how to update the file from user input. Also, I'm not using Oracle.

Thanks


Report Offensive Follow Up For Removal

Response Number 4
Name: Luke Chi
Date: May 22, 2005 at 17:10:20 Pacific
Subject: Scripting problem
Reply: (edit)

I have very important projects to work on next week. I hope that some other people will help you with the rest.

0. to create a new display.txt file at the beginning:

> display.txt

1. display.txt file:

Monday: go to the office
Tuesday: go to the office

2. display.sh file:

#!/bin/sh

# change DAY value to test it

DAY=Wednesday

# New reminder line

LINE="$DAY: go home"

# try to replace the line using the $DAY and save the result to tmp.txt

sed -e "s/$DAY: [a-zA-Z0-9 ,.()-]*$/$LINE/" display.txt > tmp.txt

# check if the line is saved

grep "$LINE" tmp.txt > /dev/null

# if $LINE is not saved, then save it again

if [ $? -ne 0 ]; then
echo $LINE >> tmp.txt
fi

cp tmp.txt display.txt

3. check display.txt content after each test run.


Luke Chi


Report Offensive Follow Up For Removal

Response Number 5
Name: D James
Date: May 22, 2005 at 18:38:29 Pacific
Subject: Scripting problem
Reply: (edit)

Thanks very much for the help I should be right for the rest of it.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Scripting problem

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software