LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need help on if function (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-on-if-function-4175483546/)

zkyzky 11-06-2013 01:02 AM

need help on if function
 
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"


evo2 11-06-2013 01:14 AM

Hi,

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.

Evo2.

zkyzky 11-06-2013 01:35 AM

OK i tried on ur codes by doing these,
if [ "$Type" = "1" ]
elif [ "$Type" = "2" ]
else [ "$Type" = "3" ]

now it outputs it is a commission employee for all users
and it doesn't run the wage function

evo2 11-06-2013 01:44 AM

Hi,

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

Evo2.

zkyzky 11-06-2013 01:49 AM

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

Firerat 11-06-2013 01:58 AM

your script is broken

$Type is not 1 , 2 or 3

since you are using arithmetic comparison , it could be that you have trailing 'junk' in your data file
Probably a DOS EOL ( end of line )

have a look at your data file
Code:

cat -A data.txt
you may see something like

Code:

Daniel,CIO,IT,2^M$
which indicates it is in windows text format

evo2 11-06-2013 02:01 AM

Hi,

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.

Evo2.

zkyzky 11-06-2013 02:04 AM

Firerat u are right it has ^I$.
how do i solve it

zkyzky 11-06-2013 02:06 AM

hi evo2
i found out that the codes are not reading the 2 from
Daniel,CIO,IT,2

how do i make it read the 2 which is like the fourth segment from
${Name},${ID},${HP},${Type}

Firerat 11-06-2013 02:22 AM

^I$ is a tab, not what I expected :)

how do you fix 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>


Some links to help with rewriting your script
Start with this..
http://mywiki.wooledge.org/BashGuide


then these

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://www.tldp.org/LDP/abs/html/
http://www.gnu.org/software/bash/manual/bashref.html

zkyzky 11-06-2013 02:38 AM

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"

Firerat 11-06-2013 02:55 AM

I should have removed that bit =

use the second bit, the Type=$()
it is much better ( but not ideal )


All times are GMT -5. The time now is 10:23 AM.