Hey guys, i was looking for some examples of how can i check if the use input is just using numerical. i came across an example using tr :
echo "read this"
read this
if [ -z "`echo "$this" | tr -d [[:digit:]]`" ] ;
then
echo "True - only alpha and numeric"
else
echo "False - there are NON alpha and numeric stuff here!"
fi
Could somebody explain to me how the code works and why -z and -d must be used.
its the -d command which i do not get it. a syntax for a normal tr command would be
tr [set1][set2]
so how does it work in the code above. basically, the input of $this is being deleted only if it is a numerical number since we specified [[:digit:]]?. am i getting it right?
hey admad, thanks for your solution. could i ask, why must we surround the option with +()?
---------- Post updated at 03:47 AM ---------- Previous update was at 03:41 AM ----------
There another problem which i am facing. I would like to key in a a input which gets to read decimals and numerical numbers like 23.23 or 24. but i would also like to do a check to make sure that no characters like "ad.23" or "aec" is being inputted.
Some versions of sed, ed, and ex support escaped versions of the extended Regular Expressions
described above, as do the GNU utilities.
POSIX Character Classes. [:class:]
This is an alternate method of specifying a range of characters to match.
�
[:alnum:] matches alphabetic or numeric characters. This is equivalent to A-Za-z0-9. �
[:alpha:] matches alphabetic characters. This is equivalent to A-Za-z. �
[:blank:] matches a space or a tab. �
[:cntrl:] matches control characters. �
[:digit:] matches (decimal) digits. This is equivalent to 0-9. �
[:graph:] (graphic printable characters). Matches characters in the range of ASCII 33 - 126. This
is the same as [:print:], below, but excluding the space character.
�
[:lower:] matches lowercase alphabetic characters. This is equivalent to a-z. �
[:print:] (printable characters). Matches characters in the range of ASCII 32 - 126. This is the
same as [:graph:], above, but adding the space character.
�
[:space:] matches whitespace characters (space and horizontal tab). �
[:upper:] matches uppercase alphabetic characters. This is equivalent to A-Z. �
[:xdigit:] matches hexadecimal digits. This is equivalent to 0-9A-Fa-f.
POSIX character classes generally require quoting or double brackets ([[ ]]).
bash$ grep [[:digit:]] test.file
abc=723
These character classes may even be used with globbing, to a limited extent.
bash$ ls -l ?[[:digit:]][[:digit:]]?
-rw-rw-r-- 1 bozo bozo 0 Aug 21 14:47 a33b
you'll find that tr -d [set1] means delete the characters in set 1.
The code above is in fact broken:
they are testing for numerical values not alphanumerical
They left out single quotes so the command was not working right:
Try this:
echo "read this"
read this
if [ -z $(echo "$this" | tr -d '[[:digit:]]') ] ;
then
echo "True - only numeric or the string was empty"
else
echo "False - there is NON numeric stuff here!"
fi
But you do not need an external command, e.g.:
echo "read this"
read this
case $this in
*[^0-9]*|"") echo "False - there is NON numeric stuff here or string is empty" ;;
*) echo "True - only numeric" ;;
esac
Hey Scrutinizer, just let me check how this code works
echo "$this" |
*this echo out the content inside $this and pipes it into the next set of command
tr -d '[[:digit:]]')
*this command will check if the data from the $this is in digits , and if it is, will delete them. if not, it will not delete them , thus the data will not be 0 and this the message that there is a non-numeric stuff is inside.