ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
I am gonna spoil your day.
Code:
this=14
that=16
#String comparison:
if [ $this == $that ]
then
fi
#Integer comparison:
if [ $this -eq $that ]
then
fi
See, no conversions made, this is correct code.
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:
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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.
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:
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
You made made open the Bash scripting guide, and you are right, the '=' and '==' operator are equal. Pun intended.
Quote:
Originally Posted by gnashly
Something like this will usually give errors when using single brackets:
if [ $this = "that" ]
then
fi
However, assuming that you really meant "that" and not "$that" (A constant and not a variable) it doesn't give errors in my version of bash which is 3.1.17. I am not sure about other versions, nor about about other shells. Quoting might be the safest way to go in most cases.
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:
The output isn't a number, but the string "8-1" Any feedback please?
Indeed, it is what you're asking to the shell since
Code:
var=$var-1
just does string concatenation. To perform an arithmetic operation you need an arithmetic operator! Post #2 in this thread provides the solution.
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).
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.