| View previous topic :: View next topic |
| Author |
Message |
jamesca
Joined: Thu Aug 17, 2006 3:38 pm Posts: 2
|
Posted: Thu Aug 17, 2006 3:47 pm Post subject: Bash |
|
|
I am having trouble with a Bash program I am trying to enter.
#/bin/bash
get_employee_info ()
{
current_empl_id=`awk $id {print $1} employee_database`
current_last_name=`awk $id {print $2} employee_database`
current_first_name=`awk $id {print $3} employee_database`
current_sex=`awk $id {print $4} employee_database`
current_birthdate=`awk $id {print $5} employee_database`
current_salary=`awk $id {print $5} employee_database`
current_tax=`awk $id {print $2} tax_table`
return 0;
}
update ()
{
echo "Do you need a list of employees?"
read list
if [ $list == "y" || $list == "Y" ]
then
cat employee_database | more
fi
echo "Which employee do you want to change?"
read id
get_employee_info
echo "Which aspect of employee $id do you wish to change:"
echo "1. Change last name"
echo "2. Change first name"
echo "3. Change sex"
echo "4. Change birthday"
echo "5. Change salary"
read choice
case $choice in
1)
echo "What is th enew last name of $id:"
read current_last_name;;
2)
echo "What is the new first name of $id:"
read current_first_name;;
3)
echo "What is the new sex of $id:"
read current_sex;;
4)
echo "What is the new birthdate of $id in mm/dd/yyyy format:"
read current_birthdate;;
5)
echo "What is the new salary of $id:"
read current_salary;;
*)
echo "Choice not understood";;
esac
awk "$id employee_database" | sed 'd'
cat "${id}\t${current_last_name}\t${current_first_name}\t${current_sex}\t${current_birthdate}\t${current_birthdate}" >> employee_database
return 0;
}
new_record ()
{
echo "Please enter the employee\'s name, last name first:"
read last_name, first_name
echo "Please enter the ${last_name}\'s id number:"
read empl_id
echo "Please enter the ${last_name}\'s sex:"
read sex
echo "Please enter ${last_name}\'s birthdate in format mm/dd/yyyy:"
read birthdate
echo "Please enter ${last_name}\'s salary"
read salary
if [ salary > 326450 ]
then
tax_rate=(echo"scale=2;$salary \* .33" | bc)
elif [ salary > 150150 ]
then
tax_rate=(echo"scale=2;$salary \* .28" | bc)
elif [ salary > 71950 ]
then
tax_rate=(echo"scale=2;$salary \* .25" | bc)
elif [ salary > 29700 ]
then
tax_rate=(echo"scale=2;$salary \* .15" | bc)
elif [ salary > 7300 ]
then
tax_rate=(echo"scale=2;$salary \* .1" | bc)
else
tax_rate=0
fi
if [ -w employee_database == 0 ]
then
cat "${empl_id}\t${last_name}\t${first_name}\t$sex\t$birthdate\t$salary" >> employee_database
else
cat "${empl_id}\t${last_name}\t${first_name}\t$sex\t$birthdate\t$salary" > employee_database
fi
if [ -w tax_table == 0 ]
then
cat "${empl_id}\t${tax_rate}" >> tax_table
else
cat "${empl_id}\t${tax_rate}" > tax_table
fi
return 0;
}
query ()
{
echo "Do you need a list of employees?"
read list
if [ $list == "y" || $list == "Y" ]
then
cat employee_database | more
fi
echo "Please enter the employee\'s id number"
read id
get_employee_info
echo;echo;echo
echo "The statistic of employee # $id are as follows:"
echo "The last name is ${current_last_name}"
echo "The first name is ${current_first_name}"
echo "The sex is ${current_sex}"
echo "The birthday is ${current_birthdate}"
echo "The salary is ${current_salary}"
echo "The tax due is ${current_tax}"
return 0;
}
menu ()
{
clear
echo "Welcome to the DataBase Program"
echo;echo;echo
any_more="y"
while [ $any_more == "y" || $any_more == "Y" ];
do
echo "Please select from an option below:"
echo "1. Input data for a new employee"
echo "2. Query for a particular employee"
echo "3. Update a record"
echo "x to exit"
read choice
case $choice in
1)
new_record;; <-- I get an error here,
2)
query;; <-- and here. Bash says that these commands can't be found. These functions are defined further up in this file. Have I defined the functions in a wrong manner?
3)
update;;
*)
return;;
esac
echo "Any more records to input?"
read any_more
done
return 0;
}
menu |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 7999 Location: Warrington, UK
|
Posted: Thu Aug 17, 2006 4:13 pm Post subject: RE: Bash |
|
|
Is the error message confidential? _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
TheDoctor LXF regular
Joined: Mon Jan 02, 2006 9:02 pm Posts: 325
|
Posted: Thu Aug 17, 2006 6:40 pm Post subject: Re: Bash |
|
|
For those who missed the precise question:
| jamesca wrote: |
case $choice in
1)
new_record;; <-- I get an error here,
2)
query;; <-- and here. Bash says that these commands can't be found. These functions are defined further up in this file. Have I defined the functions in a wrong manner?
3)
update;;
*)
return;;
esac
|
I can't see the problem at a first glance, but I'll keep thinking. |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 7999 Location: Warrington, UK
|
Posted: Thu Aug 17, 2006 7:16 pm Post subject: RE: Re: Bash |
|
|
Ah, cunningly hidden errors
It wouldn't even run for me. The first thing is the "tax_rate=(echo" assignments are missing a $. "tax_rate=$(echo"
Then the line
| Code: | | while [ $any_more == "y" || $any_more == "Y" ]; |
gave a syntax error, it should be one of
| Code: |
while [ $any_more == "y" -o $any_more == "Y" ];
or
while [ $any_more == "y" ] || [ $any_more == "Y" ];
|
The trailing semi-colon is superfluous too, but harmless.
After making those changes, it worked, at least as far as showing the menu and prompting further when each option was selected. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
jamesca
Joined: Thu Aug 17, 2006 3:38 pm Posts: 2
|
Posted: Wed Aug 23, 2006 1:31 am Post subject: |
|
|
This is the precise error it displays:
bash: new_record: command not found
What am I doing wrong? Are the curly braces in the wrong places? Should I have the word 'function' before I declare the function? What is out of place? I am using version 2.05b on a dual Power Mac G5. |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 7999 Location: Warrington, UK
|
Posted: Wed Aug 23, 2006 3:54 pm Post subject: |
|
|
Did you make the changes I posted? With those, I got no such error. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
Steogede LXF regular
Joined: Thu May 04, 2006 6:39 pm Posts: 145
|
Posted: Wed Aug 23, 2006 6:06 pm Post subject: Re: RE: Re: Bash |
|
|
Jamesca, the whole thing is riddled with little bugs. Is this something you wrote yourself, or is it a homework exercise for you to practice your debugging?
If you wrote it yourself, you must have wrote it all in one sitting without checking it worked as you went along. Have you thought about prototyping and incrimentally developing. E.g. writing a little bit and seeing if it behaves as you expect it to, it's easier to find the bugs that way. |
|
| Back to top |
|
 |
towy71 Moderator

Joined: Wed Apr 06, 2005 3:11 pm Posts: 4169 Location: wild West Wales
|
Posted: Wed Aug 23, 2006 8:40 pm Post subject: RE: Re: RE: Re: Bash |
|
|
Gah, perhaps he is practising to run government IT projects, go the whole hog and if it don't work ask for more money (they're all idiots so they give it anyway) _________________ still looking for that door into summer |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|