BASH scripting: check for numeric values
OK, I didn't know wherelse to ask this one. I have a script that I've made which takes two arguments: a pure number and a filename.
I decided to give it a little bit of flexibility and have it so that it doesn't matter what order those two arguments are put: number then file name, or file name then number. Doesn't matter. Well, all well and good. But I need to be able to take a word ($1 or $2 in this case) and figure out if it only contains numbers, if not do a Code:
echo "You complete moron! What'd you take me for!" > /dev/stderr OK, so my first idea is this (kinda of weird I know): Code:
if [ $1 = "$(echo $1 | awk '{print strtonum($0)}')" ]; then Two issues: the awk function strtonum() will believe that the string is a hexidecimal number if proceeded by a 0x. also, if it starts with a 0, then it is interpreted as an octal number. That might be a problem in the unlikely circumstance that somebody would do that. And again, if my little script became so popular that the world over uses it, some Linux newbie in the worlde will burst out in tears as his script just simply doesn't work for some hidden reason only the original programmer (me, in this case) would possibly know about. And as he asks the question over and over again "why? why? why?", the answer shall remain hidden in the way that strtonum() impliments its magic! Or, in other words: is there a better way? Is there a more common way? Anybody tried this before? Thanks in advance, ya'll! -- the dUdemaN davE |
You could test if the value is an integer, with something like :
Code:
function is_integer() { |
Thanks for the quick response! I'll try 'er out as soon as I can. Your way looks a little bit better. I think it's a better approach: you don't really analyze the text a whole lot. Kinda kool!
-- the dudeeman daVE |
I am not very happy with it though (printf error redirected to /dev/null),
maybe this is better : Code:
function is_integer() { Code:
function is_integer() { |
An idea
I use this to separate the text from the numerics in a bash script.
# In this example a file name is one of the expected inputs, a number the other. Code:
for itm in "$@" # what is accomplished by the script 1. Tests for a file name, sets a variable to that file name. 2. Tests for a number, sets a variable to that number. The sed line: - confirms an integer number exists in the text and keeps the text - drops decimal numbers or text having a period - drops text having a comma or colon - drops text having alphas The $cnt variable is tested for null and assigned the value 0 if null. I am using integers greater than zero, which is why I set $cnt to zero if no integer numbers were found as a result of the string returned by sed. I test $cnt for zero elsewhere in the rest of my script. I use the file name and numeric variables elsewhere in a custom script. Just my 2 cents... |
Moin,
try a "negative" check: If there's a non empty input after removing all digits, it's not an integer: Code:
test -z "$input" -o -n "`echo $input | tr -d '[0-9]'`" && echo NaN Code:
echo $input | grep -q '^-\?[0-9]\+$' || echo NaN |
how about
Code:
if ! [ "$1" -eq "$1" 2> /dev/null ] |
Assuming that "number" is "unsigned integer" then the simplest way to check whether $1 holds a number is
Code:
if [[ $1 =~ ^[0-9]+$ ]]; then |
I was intrigued by finding a better solution than my lightweight version, and so created a script that runs a series of tests to validate data. The types checked are file, integer decimal, comma delimited integer, comma delimited decimal. After burning through some serious kludge versions, I arrived at some positive results. See what you think.
Code:
#!/bin/sh |
Thanks for sharing :)
The + is missing after ] in ${arg//[^0-9]/} |
Hello, your welcome. Hope the code is interesting/of use.
Actually this is correct: [[ $arg == ${arg//[^0-9]/} ]] The statement is substituting nothing for any characters that are not 0-9, so that integer numbers only will pass through the substitution unchanged, and this will make the terms equal in characters and test bracket test condition passes; but any non numerics will be substituted out of the right side argument, which makes the bracket test condition fail. Note that nothing follows the farthest right slash, which is what is substituted. The double slash indicates to substitute all matching characters specified in the test. Quick example: Input: 1500 Test Output: 1500 Input == Output Pass Input: 8.25 Test Output: 825 Input != Output Fail Here is a great source for string ops (my fav): http://tldp.org/LDP/abs/html/refcards.html#AEN21811 |
You can use different methods to validate integer in bash script.
Check bellow ling for 4 different methods. How to validate integer in shell script |
All times are GMT -5. The time now is 02:40 PM. |