LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-06-2013, 02:02 AM   #1
zkyzky
LQ Newbie
 
Registered: Nov 2013
Posts: 7

Rep: Reputation: Disabled
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"
 
Old 11-06-2013, 02:14 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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.
 
Old 11-06-2013, 02:35 AM   #3
zkyzky
LQ Newbie
 
Registered: Nov 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
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

Last edited by zkyzky; 11-06-2013 at 02:39 AM.
 
Old 11-06-2013, 02:44 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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.
 
Old 11-06-2013, 02:49 AM   #5
zkyzky
LQ Newbie
 
Registered: Nov 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
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

Last edited by zkyzky; 11-06-2013 at 02:51 AM.
 
Old 11-06-2013, 02:58 AM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
Old 11-06-2013, 03:01 AM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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.
 
Old 11-06-2013, 03:04 AM   #8
zkyzky
LQ Newbie
 
Registered: Nov 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Firerat u are right it has ^I$.
how do i solve it
 
Old 11-06-2013, 03:06 AM   #9
zkyzky
LQ Newbie
 
Registered: Nov 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
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}
 
Old 11-06-2013, 03:22 AM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
^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

Last edited by Firerat; 11-06-2013 at 03:24 AM.
 
Old 11-06-2013, 03:38 AM   #11
zkyzky
LQ Newbie
 
Registered: Nov 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
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"
 
Old 11-06-2013, 03:55 AM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
I should have removed that bit =

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can a friend function access a public function from a base class? lesca Programming 1 11-17-2010 02:56 PM
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 11:10 PM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 06:00 PM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 02:21 AM
Compilation issue when Function is parameter in function call on LINUX sa20358 Linux - Software 2 07-24-2008 11:19 PM


All times are GMT -5. The time now is 05:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration