Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Bash

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
jamesca



Joined: Thu Aug 17, 2006 3:38 pm
Posts: 2

PostPosted: Thu Aug 17, 2006 3:47 pm    Post subject: Bash Reply with quote

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
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8439
Location: Warrington, UK

PostPosted: Thu Aug 17, 2006 4:13 pm    Post subject: RE: Bash Reply with quote

Is the error message confidential?
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
TheDoctor
LXF regular


Joined: Mon Jan 02, 2006 9:02 pm
Posts: 325

PostPosted: Thu Aug 17, 2006 6:40 pm    Post subject: Re: Bash Reply with quote

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
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8439
Location: Warrington, UK

PostPosted: Thu Aug 17, 2006 7:16 pm    Post subject: RE: Re: Bash Reply with quote

Ah, cunningly hidden errors Sad

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.
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
jamesca



Joined: Thu Aug 17, 2006 3:38 pm
Posts: 2

PostPosted: Wed Aug 23, 2006 1:31 am    Post subject: Reply with quote

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
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8439
Location: Warrington, UK

PostPosted: Wed Aug 23, 2006 3:54 pm    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
Steogede
LXF regular


Joined: Thu May 04, 2006 6:39 pm
Posts: 146

PostPosted: Wed Aug 23, 2006 6:06 pm    Post subject: Re: RE: Re: Bash Reply with quote

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
View user's profile Send private message
towy71
Moderator


Joined: Wed Apr 06, 2005 3:11 pm
Posts: 4254
Location: wild West Wales

PostPosted: Wed Aug 23, 2006 8:40 pm    Post subject: RE: Re: RE: Re: Bash Reply with quote

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 user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast