Converting string to integer in BASH
lets say i want to convert a string "76" to integer
76. How would I do this in BASH? I remember in Pascal I could do something like char ('7')+39 or some such, and that would convert between character and a number corresponding to that character. Can't seem to be able to figure this out in BASH. What I actually wanted to do was to compare two numbers one of which I got from top command. I finally figured out that I could compare two strings as well, so the problem is solved. But it would be nice to know how to convert between integers and strings. thanks a bunch in advance. |
I don't think you need to actually convert anything...
teddy@toshiba~$ a="76" teddy@toshiba~$ echo $((a+3)) 79 teddy@toshiba~$ echo $((a-12)) 64 teddy@toshiba~$ |
This is covered in the Advanced Bash-Scripting Guide: 4.3. Bash Variables Are Untyped.
|
I am gonna spoil your day.
Code:
this=14 Even if you get $this and $that passed as cmd line parameters. The advantage of the second comparison is that you can find out if one varibale is greater than the other: Code:
if [ $this -gt $that ] |
Actually string comparisons should be quoted:
if [ "$this" = "$that" ] then fi |
Not necessarily. Quoting is needed when to avoid errors when strings contain certain characters. So it is better practice. But for strings containing [0-9a-zA-Z] non quoted comparision works fine. For the sake of clearness and to show the untypedness (?) I advertently omitted the quotes here, although I always use them in production code.
BTW, you forgot a '=' in the comparison. ;) jlinkels |
Quote:
|
I purposely left out the extra '=' as it seems to be irrelevant to the purpose of the code. Also, I know you can get way with not quoting strings, but it is good practice -especially when using single brackets.
Something like this will usually give errors when using single brackets: Code:
if [ $this = "that" ] |
You made made open the Bash scripting guide, and you are right, the '=' and '==' operator are equal. Pun intended.
Quote:
jlinkels |
a=expr'$a/1'
Not sure of the quotes, but this is what the OP wants. I tried something like this once for some iteration and noted that adding 0.0 didn't work. a=expr'$a+0.000' gave someting like 760.0000 End |
Quote:
|
Surely conversion isn't needed?
I'm on GNU bash, version 4.0.33(1)-release (i486-pc-linux-gnu)
In my shell script, I need to use the static number of cores available in the system minus one; I'm doing the following: Code:
var=`grep -c 'core id' /proc/cpuinfo` |
Quote:
Code:
var=$var-1 An aside note: please, don't resurrect old threads like this. Better to start a new one. By the way, the Advanced Bash Scripting Guide already have all the answers to your question (see specifically §4.3, chapter 13 and §8.3). |
Quote:
Code:
let var=var-1 Code:
let var-- EDIT: you beat me to it, colucix :) EDIT2: I didn't realise we were resurrecting the dead! 2007 and all that! |
Quote:
http://www.tldp.org/LDP/abs/html/ |
All times are GMT -5. The time now is 03:48 AM. |