arithmetic in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting arithmetic in ksh
# 1  
Old 01-30-2007
arithmetic in ksh

Helloo..

I am trying one very simple thing I could not find anything on google..


I have 2 integer variable..and I need to do division...in ksh
where $catch and $num are integer variable..

I tryed with this:

printf "%0.2f" $final=$catch/$num

but it does not work..

any help is welcome..
# 2  
Old 01-30-2007
try this one
Code:
export final=`expr $catch / $num`
echo $final

You might also use bc ...
# 3  
Old 01-30-2007
Code:
$x=4
$y=2
$expr $x / $y
2
$echo $(( x / y ))
2
$echo $x / $y | bc
2


Last edited by anbu23; 01-30-2007 at 08:06 AM..
# 4  
Old 01-30-2007
Rightttt but this is not wanted result..

I tryed also bc I forgot to mention that..

but my $catch is 14 $num is 100

it is defined above in program..


result should be 0,14 also I tryed this:

x=`echo $catch / $num | bc`
echo $x

but this does not give goot results..
# 5  
Old 01-30-2007
Code:
$echo "scale=2; 14 / 100" | bc
.14

# 6  
Old 01-30-2007
well..looks lke that is it..

thanks for quick info..
# 7  
Old 01-30-2007
Quote:
Originally Posted by anbu23
Code:
$x=4
[...snip...]

$echo $(( x / y ))
2

[...snip...]

Isn't this the "preferred" way of doing arithmetic in ksh and bash? I recall reading that it doesn't spawn a new shell like expr and bc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arithmetic with bash

I need to divide the number of white spaces by total number of characters in a file using bash. I am able to get the number of white spaces correctly using: tr -cd “”¯ < afile | wc -c I am also able to get the total number of characters using: wc -c afile How do I divide the first... (2 Replies)
Discussion started by: ngabrani
2 Replies

2. Shell Programming and Scripting

Need help with date arithmetic please

Hello fellow forum members, I wrote below piece of code to calculate the date after a given date - date=$DATE_FINAL declare -a max_month=(0 31 28 31 30 31 30 31 31 30 31 30 31) eval $(echo $date|sed 's!\(....\)\(..\)\(..\)!year=\1;month=\2;day=\3!') (( year4=year%4 )) (( year100=year%100... (9 Replies)
Discussion started by: ektubbe
9 Replies

3. Shell Programming and Scripting

ksh Arithmetic syntax error while comparing decimal numbers

Hello, I am having a problem when i execute following script on RHEL 6.4. Same script works fine on another machine where I have same version of RHEL and KSH. Below is the rpm and RHEL version. ossvm12(0)> rpm -qa | grep ksh ksh-20100621-19.el6.x86_64 ossvm12(0)> cat... (7 Replies)
Discussion started by: Adithya Gokhale
7 Replies

4. Programming

Pointer Arithmetic In C

I have a fundamental question on C pointer arithmetry.. Suppose i have a c string pointer already pointing to a valid location, Can I just do a charptr = charptr +1; to get to the next location, irregardless if my program is 32 or 64 bits? or should i do it this way: charptr =... (1 Reply)
Discussion started by: Leion
1 Replies

5. Shell Programming and Scripting

Arithmetic operations in bash,ksh,sh

Guys, The below expression is valid in which shells (sh,ksh,bash,csh)? VAR1=2 VAR2=$(($VAR1 -2)) Thanks (1 Reply)
Discussion started by: rprajendran
1 Replies

6. UNIX for Dummies Questions & Answers

Arithmetic: how to??

Hello all, I'd like to know how to perform arithmetic on multiple files. I have got many tab-delimited files. Each file contains about 2000 rows and 2000 columns. What I want to do is to to sum the values in each row & column in every file. The following explains what I want to do; ... (9 Replies)
Discussion started by: Muhammad Rahiz
9 Replies

7. Shell Programming and Scripting

ksh-script "arithmetic syntax error" comparing strings

Hi all, I´ve already searched the forum but can´t find what i am doing wrong. I am trying to compare two variables using ksh under red hat. The error I get is: -ksh: .: MDA=`md5sum /tmp/ftp_dir_after_transfer | cut -d' ' -f1 ` MDB=`md5sum /tmp/ftp_dir_before_transfer | cut -d' ' -f1 `... (3 Replies)
Discussion started by: old_mike
3 Replies

8. Shell Programming and Scripting

strange ksh arithmetic

Hello, I would like to understand this... I'm using ksh and doing (( z = y - 1 )) if y=34, then the result for z is 33, but if y=034 the result is z=27. Why?? Thanks (15 Replies)
Discussion started by: fbg
15 Replies

9. Shell Programming and Scripting

Help with arithmetic operation

I am using egrep to extract numbers from a file and storing them as variables in a script. But I am not able to do any arithmetic operations on the variables using "expr" because it stores them as char and not integers. Here is my code and the error I get. Any help will be appreciated. #!/bin/sh... (3 Replies)
Discussion started by: emjayshaikh
3 Replies

10. UNIX for Dummies Questions & Answers

Arithmetic In UNIX

I am a beginner and I have been searching the web all morning for this answer but can't find it anywhere. I know that prtint 1*2 will return 1*2 what if i actually want the problem to be calculated so i want print 1*2 to return 2 How is this done? (3 Replies)
Discussion started by: tygun
3 Replies
Login or Register to Ask a Question