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 and shell scripting languages here.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-23-2006
ragha81 ragha81 is offline
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..
  #2 (permalink)  
Old 10-23-2006
Manish Jha Manish Jha is offline
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
  #3 (permalink)  
Old 10-23-2006
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,802
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 (permalink)  
Old 10-23-2006
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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 (permalink)  
Old 10-23-2006
ragha81 ragha81 is offline
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

  #6 (permalink)  
Old 10-23-2006
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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 (permalink)  
Old 10-23-2006
ragha81 ragha81 is offline
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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0