Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to read this line from a text file eg.
Daniel,CIO,IT,2
i am trying to read the last letter 2, as i named it as a salary type.
but it always outputs as the type 1 salaried employee.
and i want to store the wage to the person. how can i do that
Code:
enter_payroll()
{
gross=0;
echo "[Option: $input]"
echo "Enter payroll of an employee"
echo "**********************************"
echo "Enter employee's name:"
read Name
#Retrieve current entry into individual fields
line=`grep -i "$add_employee" $PAYROLL`
Type=`echo $line | cut -d "," -f4`
#Check if entry to update exist in records
if [ `count_lines "^${Name},"` -eq 0 ]
then
echo "Error: This particular record does not exist!!"
else
locate_lines "^${Name}"
read Type
if ((Type = "1"));
then
echo "He is an Salaried employee"
echo "Enter hourly wage: "
read wage
echo "Enter hours worked this week: "
read hours
gross=`echo "$wage*$hours" | bc`
echo "Gross wages: $" $gross
elif ((Type = "2"));
then
echo "He is an Hourly employee"
else ((Type = "3"));
echo "He is an Commission employee"
I'm guessing this is supposed to be part of a bourne shell script. The first problem I see here is inconsistent indentation, making it extremely difficult to work out how deep in nested "if" blocks the code is at any point. However the main problem are the lines like:
Code:
if ((Foo = "666"));
which presumably should be:
Code:
if [ "$Foo" = "666" ]
So, please try to make those modifications, and post back if you still have problems.
you need to learn to debug your script. At this point I would be directly inspecting the value held by the the $Type variable. Eg, after you set it do something like:
Code:
echo "The variable Type is set to: $Type"
I also see another bug:
Code:
else [ "$Type" = "3" ]
should be
Code:
elif [ "$Type" = "3" ]
This also hints at good practice for catching unexpected values of $Type (similar to above). Eg add something like the following as the last block in you if statement.
Code:
else
echo "Unknown value for Type: $Type"
return
fi
hmm, i added the return and it just jump straight to return and the program resets.
i removed the return and it outputs the last function.
why is this happen.
if i remove the
else
echo "Unknown value for Type: $Type"
it will also output the last function "commission employee"
seems like it is not reading the type.
I've already added what u said above and it works fine until this enter_payroll function
that function is quite strange. At one point you appear to set $Type from the file $PAYROLL and then a few lines later you appear to interactively "read" it.
in all honesty , it is not so simple...
the reason being, the whole script has problems ( I suspect, as I think I only see a small portion of it here )
a very quick and dirty fix would be
Code:
if [[ "$Type" = "1 " ]]
then
echo "He is an Salaried employee"
echo "Enter hourly wage: "
read wage
echo "Enter hours worked this week: "
read hours
gross=`echo "$wage*$hours" | bc`
echo "Gross wages: $" $gross
elif [[ "$Type" = "2 " ]]
then
echo "He is an Hourly employee"
elif [[ $Type = "3 " ]]
then
echo "He is an Commission employee"
else
echo "ERROR Type:${Type} not recognised"
return 1
fi
NOTE "1<tab>" "2<tab" "3<tab>"
BUT, that is very dirty, to be honest your script needs to be rewritten from scratch, or at least fixed
another, simpler fix ( now I think about it )
Code:
Type=$( cut -d "," -f4 <<< "$line" | cut -f1 )
this works because the default delimiter with cut is <tab>
thanks for the links.
i tried ur tab codes and its not working still.
the program just straight runs to the most bottom function and output it.
like this line echo "Unknown value for Type: $Type"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.