LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2012, 08:43 PM   #1
leszer
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Rep: Reputation: Disabled
Shell - string to integer?


Hi. I'm having a heck of a time getting this to work..

I have a feeling the variable needs to be converted somehow to integer but not sure how to make this work and bc isn't an option.

Code:

#!/bin/bash


value1= cat File1
value2= cat File2
dividend= $((value2/value1))

echo $dividend

echo $value1
echo $value2
Any help appreciated. Thanks!
 
Old 07-07-2012, 09:31 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
BASH makes no distinction between data types for variables. That distinction is made only at the time that the variable is operated upon, I think.

To assign the output of a command to a variable you must use command substitution $( ... ).
Code:
value1="$(cat File1)"
Also:
Code:
dividend= $((value2/value1))    # should be
dividend= $(( value2/value1 ))
Of course your code as is will only work for only a single value in both File1 and File2.

Last edited by towheedm; 07-07-2012 at 09:35 PM.
 
Old 07-07-2012, 09:44 PM   #3
leszer
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Okay, I can see that the division is working now but it also gives me a 'command not found' when delivering the dividend.


Division.s: line 6: 4: command not found
 
Old 07-07-2012, 09:52 PM   #4
leszer
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by leszer View Post
Okay, I can see that the division is working now but it also gives me a 'command not found' when delivering the dividend.


Division.s: line 6: 4: command not found
^^ Pay no attention to this. lol Stupid space after the equal sign. Thanks, solved!
 
Old 07-08-2012, 04:21 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Just add one more item, you can actually perform the assignment within the round brackets:
Code:
(( dividend = value2 / value1 ))
And yes the spaces are fine now
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Comparing string with integer luvshines Programming 5 09-20-2010 01:23 PM
[SOLVED] How to convert string to integer back to string C++ LazerPhreak Programming 1 03-13-2010 07:32 PM
how to get integer part from string in Shell Script sunilvadranapu Programming 4 11-02-2008 04:51 AM
Convert string to integer in c hoshangi Programming 3 04-20-2008 01:32 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration