Need help with arithmetic expression using variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with arithmetic expression using variables
# 1  
Old 04-18-2009
Power Need help with arithmetic expression using variables

Hi,

I am trying to do a percentage calculation in a Bourne shell script. The following works fine, but when I try to subsitute the values 153824 and 246000 for variables (e.g. used and limit), I get errors.

calc()
{
awk 'BEGIN {OFMT="%f"; print '"$*"'; exit}'
}
calc '(153824 / 246000) * 100'

Can some one please help me with whatever it is that I am missing?
Thanks!
# 2  
Old 04-18-2009
hi,
you need to quote the variables:
calc()
{
awk 'BEGIN {OFMT="%f"; print '"$*"'; exit}'
}
VAR1=20
VAR2=30
calc '('$VAR1' / '$VAR2') * 100'


Bye
# 3  
Old 04-18-2009
Quote:
Originally Posted by shorehil
Hi,

I am trying to do a percentage calculation in a Bourne shell script. The following works fine, but when I try to subsitute the values 153824 and 246000 for variables (e.g. used and limit), I get errors.

calc()
{
awk 'BEGIN {OFMT="%f"; print '"$*"'; exit}'
}
calc '(153824 / 246000) * 100'

Can some one please help me with whatever it is that I am missing?
Code:
calc "($used / $limit) * 100"

# 4  
Old 04-18-2009
Hello sauron, cfajohnson,
Both methods worked! Thank you. This is my first time using a forum so you have made this a positive experience. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

SHELL: UNIX : Ls regular expression not working when used with variables

If i do below command in unix prompt which static values (ie 27..97), it is working fine and gives desired output >ls -d $WORKDIR/batch/somefilename_{27..97}.* 2>/dev/null somefilename_27.sometxt somefilename_28.sometxt somefilename_29.sometxt .. somefilename_97.sometxt But if i want... (2 Replies)
Discussion started by: haiderali
2 Replies

2. Shell Programming and Scripting

Get variables from expression

Hi, I have a k-shell master script which reads variables that are globally set and this script is modified by users per their requirements and users define few other variables. In the script few expressions are evaluated which include both global variables and those added by the user, I... (8 Replies)
Discussion started by: zulfi123786
8 Replies

3. Shell Programming and Scripting

PAssing variables to awk arithmetic

Hi all, I am wanting to pass variables from a file to an awk arithmetic formula. When I use the formula with the value it works well. As soon as I make these variables I get an inf (infinity) response. I can certainly echo the variables back and they look correct. My googling for answers has... (3 Replies)
Discussion started by: gafoleyo73
3 Replies

4. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

5. Shell Programming and Scripting

checking variables + arithmetic operator help

i'm trying to make a script to simply add numbers together for example i have a file script file called add add 1 2 3 4 5 15 however i want the user to put at least 3 numbers so i did this if then echo "please enter more than two numbers" exit 1 fi but it's telling me i have... (3 Replies)
Discussion started by: el3ctr0nic47
3 Replies

6. Shell Programming and Scripting

awk variables in regex expression ?

Hello, Could someone explain why this one returns nothing: $ x=/jon/ $ echo jon | awk -v xa=$x '$1~xa {print}' $ while the following works fine: $ x=jon $ echo jon | awk -v xa=$x '$1==xa {print}' $ jon and the following works fine: $ echo jon | awk '$1~/jon/ {print}' $ jon ... (3 Replies)
Discussion started by: vilius
3 Replies

7. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

8. Shell Programming and Scripting

Assigning expression value to tcsh variables

Hi All, I have a tcsh script as: #!/usr/bin/csh -x set packsName=$(awk -F'' '/^execute.*=true/{print $2}' ExecutePacks.config) for var in $packsName do echo "printed $var" done I want to assign the value which is returned by awk function to the variable called packsName. How do I... (2 Replies)
Discussion started by: AB10
2 Replies

9. Programming

error: initializer expression list treated as compound expression

I had seen this error for the first time ..... error: initializer expression list treated as compound expression please help.... (12 Replies)
Discussion started by: arunchaudhary19
12 Replies

10. Shell Programming and Scripting

Can I use wc -l with arithmetic expression?

Folks, I am wondering that i can use something like this in one line. For example, $((cat filename > wc -l) / 2) It doesn't work; how to get it work using command substitution? Moreover, is there any option for wc -l not to return filename after the line counts? wc -l filename would... (3 Replies)
Discussion started by: lalelle
3 Replies
Login or Register to Ask a Question