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.
what is the best way, in bash, to test if a string is a valid decimal number? hexadecimal number? octal? binary? a valid IP address? IPv4? IPv6? i would like to see one command ways.
i am thinking about making a tool to do this for bash scripting even though i have shifted over to Python for such things.
You could possibly use tricks from https://wiki.bash-hackers.org/syntax/pe to eg strip out any 'valid' chars and see if anything it left - if there is, that wasn't a valid decimal (or whatever)..
what is the best way, in bash, to test if a string is a valid decimal number? hexadecimal number? octal? binary? a valid IP address? IPv4? IPv6? i would like to see one command ways.
i am thinking about making a tool to do this for bash scripting even though i have shifted over to Python for such things.
Do not do that. bash is not really capable/suitable to do decimal or hexadecimal (octal/binary) calculations. Obviously it can be implemented (in pure bash), but it will be extremely slow/inefficient - and therefore more or less useless.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818
Rep:
Quote:
Originally Posted by chrism01
You could possibly use tricks from https://wiki.bash-hackers.org/syntax/pe to eg strip out any 'valid' chars and see if anything it left - if there is, that wasn't a valid decimal (or whatever)..
That might work for some of the more simple checks. But a one liner to tell you that whether or not "300.300.300.300" (all valid characters) is a valid IPv4 address? Not likely. Someone told me not long ago that the regex for accurately validating an IPv6 address in all its wierd and wacky forms ("Standard, and Mixed, and Compressed... Oh my!") is several thousand characters long. That'd be a heck of a one-liner.
That might work for some of the more simple checks. But a one liner to tell you that whether or not "300.300.300.300" (all valid characters) is a valid IPv4 address? Not likely. Someone told me not long ago that the regex for accurately validating an IPv6 address in all its wierd and wacky forms ("Standard, and Mixed, and Compressed... Oh my!") is several thousand characters long. That'd be a heck of a one-liner.
how can you test for write access to a file in one line? the "test -w" command can do that and give a result in the exit status. the reason i am asking this is to discover if it is worthwhile to make a new command to do such a test. so maybe "testnum -x $myhex" could do that.
note that bash can convert hexadecimal and octal to decimal in its arithmetic operation. i don't know how to trap the errors the a bad digit could cause. maybe someone here does.
i have written tests for IP4 and IP6 in Python as functions that let the C library do the hard work. testing numbers in various bases from 2 to 36 or beyond should be trivial in Python to make a command script. i'm thinking of:
Code:
testnum -b num
testnum -o num
testnum -d num
testnum -x num
testnum -4 addr
testnum -6 addr
how can you test for write access to a file in one line? the "test -w" command can do that and give a result in the exit status.
test is a shell built in function as well as a GNU core utility. The latter is a separate compiled program and works because that is how the utility was written. If it is worthwhile for you to have such a utility then who cares about what others think...
bash basically treats all variables as strings until you use them in a particular function or test which is why I assume that regex was suggested above. You could probably write your utility in just about any language but bash might not be the best choice.
how can you test for write access to a file in one line? the "test -w" command can do that and give a result in the exit status. the reason i am asking this is to discover if it is worthwhile to make a new command to do such a test. so maybe "testnum -x $myhex" could do that.
You can definitely implement it and put it into bash [source code] if you wish.
Quote:
Originally Posted by Skaperen
note that bash can convert hexadecimal and octal to decimal in its arithmetic operation. i don't know how to trap the errors the a bad digit could cause. maybe someone here does.
As you wrote a test (or convert) function will return 0 (as exit code) on success and return non-zero otherwise. What else do you need?
Quote:
Originally Posted by Skaperen
i have written tests for IP4 and IP6 in Python as functions that let the C library do the hard work. testing numbers in various bases from 2 to 36 or beyond should be trivial in Python to make a command script. i'm thinking of:
Code:
testnum -b num
testnum -o num
testnum -d num
testnum -x num
testnum -4 addr
testnum -6 addr
Additionally -p (positive), -n (negative), -f (float), -i (integer). What about scientific notations, complex numbers and others?
i don't know how to trap the errors the a bad digit could cause. maybe someone here does.
If I understand you.
Code:
#!/usr/bin/bash
#Test for hex or numeric example
n=(12 31 ae 1a 16 b5 ff 1f 12 24 2c AA Ac 1C 39 28 1f 3d 10 29 a1 A2 a3)
is_numeric() {
case "$i" in
"" | *[![:digit:]]* | 0[[:digit:]]* ) return 1;;
esac
}
is_hex() {
case "$i" in
"" | *[![:xdigit:]]* ) return 1;;
esac
}
for i in "${n[@]}"; do
if is_numeric; then
echo ""$i" is numeric"
elif is_hex; then
echo ""$i" is hex"
fi
done
#!/bin/bash
while getopts ":n:b:o:d:n:x:" options; do
case "${options}" in
n)
mynum=${OPTARG}
if ! [[ "$mynum" =~ ^[0-9]+$ ]] ; then
echo "is not number"
exit 1
fi
;;
d)
mynum=${OPTARG}
if ! [[ "$mynum" =~ ^[+-]?[0-9]+([.,][0-9]+)?$ ]] ; then
echo "not a decimal"
exit 1
fi
;;
x)
mynum=${OPTARG}
if ! [[ "$mynum" =~ ^[A-Fa-f0-9]+$ ]] ; then
echo "is not hex"
exit 1
fi
;;
esac
done
It depends on what or if you use some standard notation for octal or hex numbers...
#!/bin/bash
while getopts ":n:b:o:d:n:x:" options; do
case "${options}" in
n)
mynum=${OPTARG}
if ! [[ "$mynum" =~ ^[0-9]+$ ]] ; then
echo "is not number"
exit 1
fi
It depends on what or if you use some standard notation for octal or hex numbers...
regex is extremely slow in bash, also would be faster if implemented as function (and sourced)
Code:
function isint() {
local mynum="$1"
local a="${mynum//[0123456789]/}"
[[ ${#a} -eq 0 ]]
}
function ishex() {
local mynum="$1,,"
local a="${mynum//[0123456789abcdef]/}"
[[ ${#a} -eq 0 ]]
}
but you need to check additionally the decimal point, +- or any other "requirements". (not tested)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.