In a bash script I need to verify that the user inputs actual numbers so I have thought the easiest way to make myself sure about that is implementing a case
:
case $1 in
[0-9]*)
echo "It's ok"
;;
*)
echo "Ain't good!"
exit 1
;;
esac
But I'm having hard time with file globbing because I can't find a way to demand the $1 value has to be numeric only. Or another way could be excluding all the alternatives:
case $1 in
-*)
echo "Can't be negative"
exit 1
;;
+*)
echo "Must be unsigned"
exit 1
;;
*[a-zA-z]*)
echo "Can't contain letters"
exit 1
;;
esac
The thing is in this case I should be able to block "special" chars like ! ? ^ = ( ) and so forth... I don't know how to acheive it. Please anyone give me a hint?
In filename globbing, just as in MS-DOS wildcarding, the shell attempts to replace metacharacters appearing in arguments in such a way that arguments specify filenames. Filename globbing makes it easier to specify names of files and sets of files.
You can use different types of globbing patterns for searching particular content from a file. 'grep' command is used for content searching in bash.
Globbing is the operation that expands a wildcard pattern into the list of pathnames matching the pattern. Matching is defined by: A '?' (not between brackets) matches any single character. A '*' (not between brackets) matches any string, including the empty string.
In computer programming, glob (/ɡlɑːb/) patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command mv *. txt textfiles/ moves ( mv ) all files with names ending in . txt from the current directory to the directory textfiles .
Actually it would be better to use
*[!0-9]*
instead of
*[^0-9]*
as the first one is POSIX and the second one is a bashism[1].
[1] http://rgeissert.blogspot.com/2013/02/a-bashism-week-negative-matches.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With