Byrone

Peen Meister
Dec 19, 2005
30,778
Almost took a well known slut home. My sister tells me she's bad news. So we have a date on Sunday.


Please discuss.
Do it at her place, just in case she steals while you're busy & she can clean her own dirty sheets when you leave.

Enjoy.


Working on a saturday sucks!:scarf:
 

icemaη

Rab's Husband - The Regista
Moderator
Aug 27, 2008
36,454
:D
I can't believe its been 5 weeks since I quit work. Time sure does fly.

Martin, whats the easiest way to check whether a variable is a number using shell commands?
 

Martin

Senior Member
Dec 31, 2000
56,913
icεmαή;2722014 said:
:D
I can't believe its been 5 weeks since I quit work. Time sure does fly.

Martin, whats the easiest way to check whether a variable is a number using shell commands?
Easiest I can think of is using grep with a regular expression to check that the thing you pass to grep is a sequence of digits, from beginning to end.

$ echo 43 | egrep "^[0-9]+$"

Egrep is grep in full regular expression mode. Here we say "find the beginning of the string with ^". Then "I want a character between 0 and 9 (ie. all the digits)". [] is used to say "between x and y". Then comes + which means "one or more such characters (ie. one or more digits)". Then we say "find the end of the string with $".

On success, grep returns 0, which you can test.

I googled and I couldn't find anything that would be more direct, here's stackoverflow:
http://stackoverflow.com/questions/806906/how-do-i-test-if-a-variable-is-a-number-in-bash

The most upvoted answer there does the same thing but in the shell itself. I prefer to use grep because I know it better.
 

icemaη

Rab's Husband - The Regista
Moderator
Aug 27, 2008
36,454
Ah okay. I wanted to know if there was a more direct way, I guess not. I used this for both integer and decimals, but it is indirect.
$(echo $num | sed 's/[0-9]*//g' | sed 's/\.//')
 

Martin

Senior Member
Dec 31, 2000
56,913
icεmαή;2722027 said:
Ah okay. I wanted to know if there was a more direct way, I guess not. I used this for both integer and decimals, but it is indirect.
$(echo $num | sed 's/[0-9]*//g' | sed 's/\.//')
The shell and most of the userland unix tools are basically string oriented, there is no strong notion of a type. This is both an advantage and a problem, because most of the time you mess with strings anyway, and it's a shortcut. If you want to do something with numbers it's inconvenient.

Another method is to use an integer test on the variable. If it's not a number the test will fail with an error message. I don't like this method as much, because it's relying on a side effect (that the test will produce the error message). So it's more obscure.

$ test $num -eq $num
bash: test: s: integer expression expected
 

icemaη

Rab's Husband - The Regista
Moderator
Aug 27, 2008
36,454
The shell and most of the userland unix tools are basically string oriented, there is no strong notion of a type. This is both an advantage and a problem, because most of the time you mess with strings anyway, and it's a shortcut. If you want to do something with numbers it's inconvenient.

Another method is to use an integer test on the variable. If it's not a number the test will fail with an error message. I don't like this method as much, because it's relying on a side effect (that the test will produce the error message). So it's more obscure.

$ test $num -eq $num
bash: test: s: integer expression expected
One would think that a script such as finding the greatest of three numbers would be quite straight forward because its one of the basic things you'd do when you are learning a new language. I've used shell scripting for quite some time now but it was always manipulation of strings in one way or the other. Never noticed that manipulating numbers can be a little complex. I've to say it brought a smile to my face when I finished :D
 

Martin

Senior Member
Dec 31, 2000
56,913
icεmαή;2722035 said:
One would think that a script such as finding the greatest of three numbers would be quite straight forward because its one of the basic things you'd do when you are learning a new language. I've used shell scripting for quite some time now but it was always manipulation of strings in one way or the other. Never noticed that manipulating numbers can be a little complex. I've to say it brought a smile to my face when I finished :D
I don't really like those kinds of exercises, because comparing numbers is not something I ever do in a shell script. Nor is it what shell scripts generally are for. So to me it's an exercise that doesn't reflect the usage and deliberately painful by using the shell for something that it's not good at. It breaks the basic "use the right tool" guideline.

Still, I love the shell for how portable it is and I have written stuff that would be much better in a better language. Biggest was probably this:
http://www.matusiak.eu/numerodix/blog/index.php/2009/06/14/networktest-improved-network-detection/

But this is pretty painful in the shell.
 

icemaη

Rab's Husband - The Regista
Moderator
Aug 27, 2008
36,454
I don't really like those kinds of exercises, because comparing numbers is not something I ever do in a shell script. Nor is it what shell scripts generally are for. So to me it's an exercise that doesn't reflect the usage and deliberately painful by using the shell for something that it's not good at. It breaks the basic "use the right tool" guideline.

Still, I love the shell for how portable it is and I have written stuff that would be much better in a better language. Biggest was probably this:
http://www.matusiak.eu/numerodix/blog/index.php/2009/06/14/networktest-improved-network-detection/

But this is pretty painful in the shell.
Thats quite a nice tool there :tup:

Gotta write some homework :andy2:
well it won't write itself :D
 

Bozi

The Bozman
Administrator
Oct 18, 2005
22,749
wassup peeps?

its the weekend and i have absolutely no plans whatsoever,well apart from a little tidying and then maybe get a few hours gaming
 

Users Who Are Viewing This Thread (Users: 0, Guests: 384)