Click to See Complete Forum and Search --> : artimetic in shell??


Linux_cat
01-20-2005, 06:21 AM
IS there a command that will let you do arithmetic in shell?? e.g

1 * 100 - 56 / 7

thanks dudes.

the.spike
01-20-2005, 07:17 AM
Check out the expr command.. man expr

It should do what you want..

There's also a syntax that looks like ((...)) where the maths goes between the brackets..

spike...

Pierre Lambion
01-20-2005, 08:08 AM
Why not use bc ? You probably want to set the decimals in a config file.

P.

Linux_cat
01-20-2005, 09:55 AM
thanks for your help, i am using the let comman but have a little prob I cant get the following script to work

#!/bin/ksh

YMOUT15=${1:?"requires an argument" }

line_count=$(zcat $YMOUT15|wc -l)

echo $line_count

percentage=$(let "line_count/100")

echo $percentage

percentage_15=$(let "percentage*15")

echo $percentage_15

zcat $YMOUT15 > YMOUTSPLIT15

split -l $percentage_15 YMOUTSPLIT 15


it seems my variables are not passing into the let commands, what am I doing wrong??.

the.spike
01-20-2005, 01:21 PM
Your script wants to be more like this :

#!/bin/ksh
YMOUT15=${1:?"requires an argument" }
line_count=$(zcat $YMOUT15|wc -l)
echo $line_count
let percentage="${line_count}/100"
echo $percentage
let percentage_15="${percentage}*15"
echo $percentage_15
zcat $YMOUT15 > YMOUTSPLIT15
split -l $percentage_15 ${YMOUTSPLIT} 15

I've hi-lighted the changes that I've made. Your let construct was wrong and you weren't qualifying your variables properly.

spike...

bsh152s
01-20-2005, 01:38 PM
I don't believe "let" will let you divide. Well, it will, but it will truncate the decimal. Use bc instead.

the.spike
01-20-2005, 06:51 PM
Originally posted by bsh152s
I don't believe "let" will let you divide. Well, it will, but it will truncate the decimal. Use bc instead.

True enough about "let" but then in this case the decimal is not important.

To be honest, if it was me, I'd have probably done the maths in awk.. don't know why, that's just what I do sometimes..

percentage_15=$( echo "$line_count | awk '{ printf (" %d\n", ( $1 \ 100 ) * 15 ) }' )

Architect
01-21-2005, 07:48 AM
Wow! I had never heard of this command before. What exactly does the 'let' command do?
Hmm..... 'let' me try a 'man let' :p

Linux_cat
01-27-2005, 05:19 AM
Thanks for the help people, much appreciated. I completed the script as below using the (()) braackets, although i may now re-write it using the awk method, thats way cool:

#!/bin/ksh

YMOUT15=${1:?"requires an argument" }

line_count=$(cat $YMOUT15|wc -l)

echo $line_count

percentage=$(((line_count/100)))

echo $percentage

percentage_15=$(((percentage*15)))

echo $percentage_15

cat $YMOUT15 > YMOUT15SPLIT
split -l $percentage_15 YMOUT15SPLIT 15

bwkaz
01-27-2005, 09:01 PM
Originally posted by Linux_cat
although i may now re-write it using the awk method, thats way cool Cool, maybe. But it wastes a process every time you start up awk. ;) Doing arithmetic in bash with $((...)) doesn't waste a process. And as long as you don't need fractional precision or arbitrarily large numbers, it'll work just as well.