LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 04-19-2013, 03:28 PM   #1
BlackWhite
LQ Newbie
 
Registered: Apr 2013
Posts: 1

Rep: Reputation: Disabled
Shell script -- Convert string to number for subtraction


Hi! I'm totally a newbie to shell scripting. I'm trying to calculate the difference between 2 numbers which grepped from a file, which gave me error because I guess they are of type string, not numeric:

var1=timestamp:n:1111111.2222222
var2=timestamp:n:3333333.4444444

start_time=$(echo ${var1#timestamp:n:}
stop_time=$(echo ${var2#timestamp:n:}

render_time=`expr $stop_time - $start_time`
which gave me error

Please help! Thanks!
 
Old 04-19-2013, 03:53 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 1,702

Rep: Reputation: 426Reputation: 426Reputation: 426Reputation: 426Reputation: 426
I believe bash only handles integers.

You might try using perl or python instead.

The conversion from string to numbers is automatic, as determined by the operator being applied to the values.
 
Old 04-21-2013, 01:59 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


Yes, bash is integer only, as is expr, which is what you're trying to use.

(expr is completely unneeded in modern shells, btw. Everything it can do is available built in. I suggest just forgetting that it exists. )

For floating point arithmetic you need to use an external command like bc or awk.

arithmetic expressions
How can I calculate with floating point numbers instead of just integers?


You also have two completely unnecessary uses of echo (the commands for which were unclosed as well),
and $(..) is highly recommended over `..`.

Code:
var1=timestamp:n:1111111.2222222
var2=timestamp:n:3333333.4444444

start_time=${var1#*:n:}
stop_time=${var2#*:n:}

render_time=$( echo "$stop_time - $start_time" | bc )

echo "$stop_time - $start_time = $render_time"

Last edited by David the H.; 04-21-2013 at 02:05 PM. Reason: corrections
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008) drwatson_droid Programming 5 12-01-2011 10:44 AM
[SOLVED] Convert string to number without 0 in the beginning nadinnne Linux - Newbie 11 06-08-2010 08:27 AM
shell script :: how to print string that appeared maximum number of times in file ?? mayankmehta83 Linux - Newbie 3 12-07-2009 07:44 AM
shell script help - subtraction?? resolute155 Programming 6 10-18-2008 06:33 AM
convert string into number, shell mystical dervish Programming 9 08-13-2007 03:32 AM


All times are GMT -5. The time now is 06:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration