converting string to number in shell script | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

converting string to number in shell script

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-22-2012
turki_00 turki_00 is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 21 October 2012, 7:15 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Question converting string to number in shell script

Hi,

I am having a problem in converting a string to number so I can preform arithmetic operations.

timeTot=0
timeTmp=$(cat idsOutput | grep 'Run time' | cut -c 36-39)
timeTot=$[$timeTot+$timeTmp] #This is line 28
echo "total RunTime=" $timeTot


this is the error msg:

./ids2.sh: line 28: 0+1.35: syntax error: invalid arithmetic operator (error token is ".35")
total RunTime= 0
Sponsored Links
    #2  
Old 02-22-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 21 July 2014, 11:56 PM EDT
Location: India
Posts: 1,816
Thanks: 24
Thanked 519 Times in 498 Posts
Quote:
Originally Posted by turki_00 View Post
cat idsOutput | grep 'Run time'
Please post the output of cat idsOutput | grep 'Run time'
Sponsored Links
    #3  
Old 02-22-2012
turki_00 turki_00 is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 21 October 2012, 7:15 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
cat idsOutput

Commencing packet processing (pid=2103)
===============================================================================
Run time for packet processing was 1.3579 seconds
    #4  
Old 02-22-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 21 July 2014, 11:56 PM EDT
Location: India
Posts: 1,816
Thanks: 24
Thanked 519 Times in 498 Posts

Code:
#!/bin/bash
timeTot=0
timeTmp=$(grep 'Run time' input | cut -c36-39)
timeTot=`echo "$timeTot + $timeTmp" | bc`
echo "total RunTime=" $timeTot

Sponsored Links
    #5  
Old 02-22-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 16 July 2014, 11:41 PM EDT
Posts: 3,057
Thanks: 33
Thanked 657 Times in 636 Posts

Code:
$ echo "0 + `awk '/Run time/ {print $7}' idsOutput`" | bc
1.3579
$ echo "10 + `awk '/Run time/ {print $7}' idsOutput`" | bc
11.3579

Sponsored Links
    #6  
Old 02-22-2012
Shirishlnx Shirishlnx is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 20 May 2014, 3:00 AM EDT
Location: India
Posts: 112
Thanks: 9
Thanked 3 Times in 3 Posts
Try .. in your script ..

Code:
timeTot=`echo "$timeTot + $timeTmp" | bc`; echo "total RunTime=" $timeTot

--Shirish Shukla
Sponsored Links
    #7  
Old 02-22-2012
turki_00 turki_00 is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 21 October 2012, 7:15 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you balajesuri
it works

However, I am doing this code inside a loop and then taking the average (avgT).
but the output of avgT is the integer part of the solution
I tried to use the bc method for the average but it only giving me the integer part of the solution.


Here is my code:

Code:
#!/bin/bash
myLoop=$1
i=1
timeTot=0

while [ $i -le $myLoop ]
do
timeTmp=$(grep 'Run time' input | cut -c36-39)
timeTot=`echo "$timeTot + $timeTmp" | bc`
i=$[$i+1]
done

echo "total RunTime=" $timeTot
avgT=`echo "$timeTot / $myLoop" | bc`
echo "Average RunTime=" $avgT

the output

Code:
./ids2.sh 2
total RunTime= 2.85
Average RunTime= 1

the value of Average RunTime should be = 2.85/2 = 1.425
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell Script to identify the line number containing a particular "string" kk2202 Shell Programming and Scripting 2 11-23-2009 08:32 AM
How to Check given string is number in shell script? balajiora Shell Programming and Scripting 6 06-12-2009 09:06 AM
Problem in converting number in shell script amitanshu.verma Shell Programming and Scripting 4 05-27-2009 03:05 PM
Converting Shell Script to HTML davwel Shell Programming and Scripting 3 10-25-2007 10:25 PM
need help converting string to number for calculation tpatput Shell Programming and Scripting 4 04-13-2007 10:40 AM



All times are GMT -4. The time now is 10:47 AM.