Bash

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Bash

Postby jamesca » Thu Aug 17, 2006 2:47 pm

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
jamesca
 
Posts: 2
Joined: Thu Aug 17, 2006 2:38 pm

RE: Bash

Postby nelz » Thu Aug 17, 2006 3:13 pm

Is the error message confidential?
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8522
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Re: Bash

Postby TheDoctor » Thu Aug 17, 2006 5:40 pm

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.
TheDoctor
LXF regular
 
Posts: 325
Joined: Mon Jan 02, 2006 9:02 pm

RE: Re: Bash

Postby nelz » Thu Aug 17, 2006 6:16 pm

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: Select all
while [ $any_more == "y" || $any_more == "Y" ];

gave a syntax error, it should be one of
Code: Select all
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.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8522
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby jamesca » Wed Aug 23, 2006 12:31 am

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.
jamesca
 
Posts: 2
Joined: Thu Aug 17, 2006 2:38 pm

Postby nelz » Wed Aug 23, 2006 2:54 pm

Did you make the changes I posted? With those, I got no such error.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8522
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Re: RE: Re: Bash

Postby Steogede » Wed Aug 23, 2006 5:06 pm

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.
Steogede
LXF regular
 
Posts: 146
Joined: Thu May 04, 2006 5:39 pm

RE: Re: RE: Re: Bash

Postby towy71 » Wed Aug 23, 2006 7:40 pm

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
User avatar
towy71
Moderator
 
Posts: 4264
Joined: Wed Apr 06, 2005 2:11 pm
Location: wild West Wales


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests