division operation in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting division operation in shell script
# 1  
Old 10-23-2006
division operation in shell script

Hi guys

I am trying to divide 2 variables that I got during the execution of script, but I unable to divide the variables. pls help. Hers is the code.

Code:
num_tasks=`sqlplus -s batch_user/batch_user@acsblest << EOF1 
set heading off
SET FEEDBACK OFF
SET PAGESIZE 0
SET LINESIZE 900
select cp_num_run_ from fusionhc_chk where cc_alias = 'eCustomerCMEObjMgr_enu';
EOF1`

echo $num_tasks

max_tasks=`sqlplus -s batch_user/batch_user@acsblest << EOF2 
set heading off
SET FEEDBACK OFF
SET PAGESIZE 0
SET LINESIZE 900
select cp_max_task from fusionhc_chk where cc_alias = 'eCustomerCMEObjMgr_enu';
EOF2`
echo $max_tasks
per_load=$num_tasks/$max_tasks
echo $per_load
if [ "$per_load" -gt "0.95" ]
then
echo "not good"
fi
exit

i did a set -x and I get this
Code:
3
20
3/20

plz help..
# 2  
Old 10-23-2006
try this for Integer division

per_load=$(($num_tasks/$max_tasks))

This would give you only integer out put. Not sure if direct decimal o/p is possible . You could use awk to get the desired decimal out.

--Manish
# 3  
Old 10-23-2006
First off, SQL provide for division:
Code:
result=\
`sqlplus -s batch_user/batch_user@acsblest << EOF1 
set heading off
SET FEEDBACK OFF
SET PAGESIZE 0
SET LINESIZE 900
select cp_num_run_ / cp_max_task 
      from fusionhc_chk 
      where cc_alias = 'eCustomerCMEObjMgr_enu';
EOF1`
echo $result

Floating point division in scripts depends on the shell - a general solution :
Code:
result=`echo "$num_tasks / max_tasks " | bc -l`
echo $result

# 4  
Old 10-23-2006
you cannot do float math natively with most shells.

ragha.sh 3 4
ragha.sh 9 10
ragha.sh 35 36

ragha.sh:
Code:
#!/bin/ksh

typeset -i num_tasks="${1}"
typeset -i max_tasks="${2}"

typeset threshold='.95'

echo "num->[$num_tasks]"
echo "max->[$max_tasks]"

per_load=$(echo "scale=2; $num_tasks / $max_tasks" | bc)

echo "per_load->[$per_load]"

if [ "$(echo "if (${per_load} > ${threshold}) 1" | bc)" -eq 1 ]; then
   echo "not good"
fi

# 5  
Old 10-23-2006
thank you guys.. i modified the code as follows and got the output, but still i am unable to compare the two numbers, VGERSH, i know you explained this part.. but I am sorry to tell that I am unable to understand theat part. my understanding of shell script is very poor.

I used sql to do the division. here is the code and set -x output.

Code:
num_tasks=`sqlplus -s batch_user/batch_user@$SIEBEL_DB_ORACLESID << EOF2 
set heading off
SET FEEDBACK OFF
SET PAGESIZE 0
SET LINESIZE 900
select cp_num_run_ / cp_max_task 
      from fusionhc_chk 
      where cc_alias = 'eCustomerCMEObjMgr_enu';
EOF2`
echo $num_tasks
set -x
if [ $num_tasks > 0.95 ]
then
echo "not good"
fi
exit

set -x output

Code:
.15
+ [ .15 ]
+ 1> 0.95
+ echo not good
not good
+ exit

# 6  
Old 10-23-2006
you cannot compare floats natively within most of the shells either.
use the 'if' construct from my previous post:
Code:
if [ "$(echo "if (${$num_tasks} > '.95') 1" | bc)" -eq 1 ]; then
   echo "not good"
fi

# 7  
Old 10-23-2006
hi vgersh.

I used the if statement that you gave. i get the following error.

Code:
.15
+ bc
srvr_load_check.ksh[42]: "if (${$num_tasks} > '.95') 1": bad substitution
+ [  -eq 1 ]
+ exit

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to find data in three file and perform replace operation

Have three files. Any other approach with regards to file concatenation or splitting, etc is appreciated If column55(billngtype) of file1 contains YMNC or YPBC then pick the value of column13(documentnumber). Now find this documentnumber in column1(Billdoc) of file2 and grep the corresponding... (4 Replies)
Discussion started by: as7951
4 Replies

2. Shell Programming and Scripting

String operation in csh shell

Hi, to everybody i have a string which Looks like this FT47;3;1;1;;;1;09.02.2017 21:21:19;2;2 and i would like to change to value on one Position only e.g. the values on Position 6 should change to 1 nevertheyless which values was there before AIX 4.3.2.0 and csh i try... (10 Replies)
Discussion started by: Nadielosabra
10 Replies

3. AIX

AIX 6, operation on time in script

Hi I have a question, On linux (centos) I am executing a script: #!/bin/bash st1=`date "+%T"` SD=`date -u -d $st1 +"%s"` #some operations ... st2=`date "+%T"` FD=`date -u -d $st2 +"%s"` time_oper=`date -u -d "0 $FD sec - $SD sec" +"%H:%M:%S"` echo "Time script execution -... (1 Reply)
Discussion started by: primo102
1 Replies

4. Shell Programming and Scripting

Help me to perform count & group by operation in shell scripting?

Hi All, I want to display the distinct values in the file and for each distinct value how may occurance or there. Test data: test1.dat 20121105 20121105 20121105 20121105 20121106 20121106 20121106 20121105 I need to display the output like Output (2 Replies)
Discussion started by: bbc17484
2 Replies

5. Shell Programming and Scripting

Unix Script -- Unable to perform division

I'm new to scripts, i wrote the below script to capture the percentage of FreeMemory available in Linux box. Output of UsedfreeMemory is displayed as '0'. I'm expecting the output like 0.89 (or) .89 --- ( 0.89 perferable) Anyone can help me on this pls. ... (3 Replies)
Discussion started by: murali1687
3 Replies

6. Shell Programming and Scripting

How to perform floating division in shell script?

I want to perform the below division operation in shell script and round the value. val1=6000 val2=5000 res=val1/val2 ----> 1.2---> Round to 2 Please help. (3 Replies)
Discussion started by: vel4ever
3 Replies

7. Shell Programming and Scripting

Division in bash script

I use this simple command result=$(echo "7 / 9" |bc -l) echo $result .77777777777777777777 .... but I like to get 0.7777777777777777777 How can I do in order to get also the 0 ? How to use code tags when posting data and code samples. (3 Replies)
Discussion started by: emi65
3 Replies

8. Shell Programming and Scripting

Column operation : cosne and sine operation

I have a txt file with several columns and i want to peform an operation on two columns and output it to a new txt file . file.txt 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 ... (4 Replies)
Discussion started by: shashi792
4 Replies

9. UNIX for Dummies Questions & Answers

what does the operation '>&' mean in a shell script

hi, all, i saw a syntax like this usr/local/mpiexec -np 8 /home/XXXX/program_exe >& ./temp/out the output will be put into ./temp/out. I want to know the meaning of the operation symbol '>&'. Thanks (3 Replies)
Discussion started by: cy163
3 Replies

10. Shell Programming and Scripting

division in shell scripts

Iam not able to device variables in a shell script.. here is the lines that i tried.. please help.. sum = echo `expr $up / ( $down )` (6 Replies)
Discussion started by: esham
6 Replies
Login or Register to Ask a Question