LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 04-05-2009, 03:45 PM   #1
lewinmg
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Rep: Reputation: 0
linux script - convert string to an integer


im not sure why this has been so hard to search for, but i've spent a good 2 hours searching for a simple anwser to this...maybe there isnt one?

im in linux shell trying to do a script that will simply convert a string to a number..

$vi test.script

A="1234"

but now i want A to be in Integer of 1234.

is this possible. I've seen so many example that dont apply to what im doing..any help would be appreciated...thanks.

-mark
 
Old 04-05-2009, 04:08 PM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
I am not sure what exactly do you want to achieve. In bash everything is a string, there aren't any other data types. What is exactly what you need to do?
 
Old 04-05-2009, 04:11 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Darn! Beaten to it

Like Perl, PHP, JavaScript and similar languages, if a value can be interpreted as a number, then it will be treated as one when you try to use it as a number, and as a string when you try to use it as a string:
Code:
rob:~$ A="1629"
rob:~$ echo $A
1629
rob:~$ A=$(($A + 1))
rob:~$ echo $A
1630
rob:~$ A="${A} Revello Drive"
rob:~$ echo $A
1630 Revello Drive
rob:~$ A=$(($A + 1))
bash: 1630 Revello Drive + 1: syntax error in expression (error token is 
"Revello Drive + 1")
rob:~$

Last edited by Robhogg; 04-05-2009 at 04:23 PM.
 
Old 04-05-2009, 04:47 PM   #4
lewinmg
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Original Poster
Rep: Reputation: 0
ok, so really, even if a = "1234" and i try to test it against a known integer like (b=1235) and use "-lt", it'll work ok? hmmm..i'll try that.

thanks so much....for some reason i thought i had to convert it to the same type...im so used to other languages needing to know the type, i never would have thought that would work..
 
Old 04-05-2009, 05:00 PM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by lewinmg View Post
ok, so really, even if a = "1234" and i try to test it against a known integer like (b=1235) and use "-lt", it'll work ok? hmmm..i'll try that.

thanks so much....for some reason i thought i had to convert it to the same type...im so used to other languages needing to know the type, i never would have thought that would work..
Yes. That's the way bash does it. It doesn't have any proper data type separation which can be a bit confusing at first. Some examples:

Code:
if [ "$A" -lt "$B" ]; then echo "$A is lesser than $B"; fi
if [ ! "$A" -lt "$B" ]; then echo "$A is not lesser than $B"; fi
if [ "$A" -ge "$B" ]; then echo "$A is greater than or equal to $B"; fi
if [ "$A" -eq "$B" ]; then echo "$A is equal to $B"; fi
if [ "$A" == "$B" ]; then echo "$A is equal to $B"; fi
if [ "$A" == "1024" ]; then echo "$A is equal to 1024"; fi

Last edited by i92guboj; 04-05-2009 at 05:01 PM. Reason: corrections
 
Old 04-05-2009, 05:08 PM   #6
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Actually, (perhaps because it doesn't understand the difference), there are different operators in the shell for numeric and string comparisons:
Code:
String                 Numeric
[ $var1 = $var2 ]      [ $var1 -eq $var2 ]
[ $var1 != $var2 ]     [ $var1 -ne $var2 ]
[ $var1 \> $var2 ]      [ $var1 -gt $var2 ]
[ $var1 \< $var2 ]      [ $var1 -lt $var2 ]

for example:
rob:~$ if [ '1234' \> '987' ]; then echo 'Higher!'; else echo 'Lower!'; fi
Lower!
rob:~$ if [ '1234' -gt '987' ]; then echo 'Higher!'; else echo 'Lower!'; fi
Higher!
See "Other Comparison Operators" for more.

Last edited by Robhogg; 04-05-2009 at 05:18 PM.
 
Old 04-05-2009, 05:23 PM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Yes, there are different operators, but that doesn't make the data typing of bash any more solid. It only applies to a conceptual level, but for bash everything is a string.
 
  


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
Convert string to integer in c hoshangi Programming 3 04-20-2008 01:32 AM
c convert integer to string? kpachopoulos Programming 5 08-23-2006 11:57 AM
C how to convert an integer to string? totti10 Programming 10 11-10-2005 05:49 AM
convert string to integer with python Kanaflloric Programming 2 05-27-2005 11:04 AM
How do you convert a string to an integer? wbdune Linux - Newbie 11 11-01-2003 02:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:45 PM.

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