Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File globbing and matching numbers only

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?

like image 251
haunted85 Avatar asked Jul 13 '11 18:07

haunted85


People also ask

What is filename globbing?

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.

Can globbing patterns be applied to the contents of a file?

You can use different types of globbing patterns for searching particular content from a file. 'grep' command is used for content searching in bash.

What does globbing mean in Linux?

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.

What is globbing explain in depth with examples?

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 .


1 Answers

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

like image 167
wodny Avatar answered Oct 03 '22 09:10

wodny