The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
what does the operation '>&' mean in a shell script cy163 UNIX for Dummies Questions & Answers 3 05-17-2008 10:54 AM
Problem in division rochitsharma UNIX for Dummies Questions & Answers 8 08-10-2006 12:16 PM
division in shell scripts esham Shell Programming and Scripting 6 11-30-2005 02:42 AM
Cannot adjust division jav_v Filesystems, Disks and Memory 1 08-20-2002 05:50 AM
division problem inquirer Shell Programming and Scripting 3 04-02-2002 07:03 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Jul 2006
Posts: 145
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..
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Dec 2005
Location: Boston, USA
Posts: 65
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
Reply With Quote
  #3 (permalink)  
Old 10-23-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,502
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
Reply With Quote
  #4 (permalink)  
Old 10-23-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
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
Reply With Quote
  #5 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Jul 2006
Posts: 145
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
Reply With Quote
  #6 (permalink)  
Old 10-23-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
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
Reply With Quote
  #7 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Jul 2006
Posts: 145
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
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0