Bash Shell script--need help

Hi all,

i am beginner to unix and trying out a shell script which does the following. i have to calculate a persons salary. his salary is read from the keyboard. he has two types of deductions. 40% as dearness allowance and 20% as house rent. i have to print the gross salary. here is the code i've come up with. when i executed the shell script it says

expr: non-numeric argument

#!/bin/bash
echo what is your salary\?
read salary
DA=0.4
Deduction=`expr $DA \* $salary`
rent=0.2
rentdeduct=`expr $rent \* $salary`
grossalary=`expr $salary - \($Deduction + $rentdeduct\)`
echo $grossalary

thanks chaitanya

---------- Post updated at 01:26 PM ---------- Previous update was at 01:13 PM ----------

hey guys, i modified the script a little here is the code now

#!/bin/bash
echo what is your salary\?
read salary
DA=0.4
Deduction=`echo $DA \* $salary | bc`
rent=0.2
rentdeduct=`echo $rent \* $salary | bc`
grossalary=`echo $salary - \($Deduction + $rentdeduct\) | bc`
echo $grossalary

I am getting an o/p as 2400, i.e. when i enter the salary as 6000. now the script doesnt seem to calculate rentdeduct and grossalary.

thanks
chaitanya

I use ksh in my script, this is one reason - builtin float calculation support.

#!/bin/ksh
# LANG set to C = no locale delimeter , / . and so on
LANG=C
echo -n "what is your salary ?"
read salary
DA=0.4
(( Deduction=DA * $salary  ))
rent=0.2
(( rentdeduct=rent * salary  ))
(( grossalary=salary - Deduction + rentdeduct  ))
echo "$grossalary"
# or better for output
printf "%.2f\n" "$grossalary"

Or if use bash, then use bc "calculator":

value=$( echo "$num1 * $num2" | bc )

Add set -x to the script just after your bash definition to see some debugging output, you'll probably be able to find where you're making incorrect calculations.

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Why this text is added so many threads. Example this. I can't see any problem in previous messages. I hope when you are adding this constant long text using cut-paste, you add also link to the message, easier to understand your meanings. If you mean this time those bold text, then tell it only. This kind of noice without meaning make reading more heavy.

Not so soul.

this text is added cause the members don't use CODE tags. it is a rule to use these... i've edited the OP thread and added code tags... also it is not your work to do moderation. if you've questions use the proper forum (feedback) cause THIS post makes reading realy more heavy!
for direct question to a mod or admin there is also the way of using a PM instead.