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() |
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")); Code:
if [ "$Foo" = "666" ] Evo2. |
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 |
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" Code:
else [ "$Type" = "3" ] Code:
elif [ "$Type" = "3" ] Code:
else |
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 |
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 Code:
Daniel,CIO,IT,2^M$ |
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. |
Firerat u are right it has ^I$.
how do i solve it |
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} |
^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 " ]] 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 ) 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 |
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" |
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. |