Results 1 to 8 of 8
  1. #1
    chmod -x /bin/chmod
    Join Date
    Jan 2004
    Posts
    3,193

    Input Validation Shell Scripting

    Does anyone know how to do input validation in a shell script?

    e.g.

    read numOne # Need to validate this is a number only
    read letterOne # Need to validate that this is either A, B, C

  2. #2
    chmod -x /bin/chmod
    Join Date
    Jan 2004
    Posts
    3,193
    read letterOne, okay that's a stupid question - figured it out,

    What about number validation?

  3. #3
    CC International Master Brian_Jones's Avatar
    Join Date
    Nov 2003
    Location
    Australia
    Posts
    2,058
    Quote Originally Posted by skip to my lou
    read letterOne, okay that's a stupid question - figured it out,

    What about number validation?
    i guess this is unix shell scripting right (or are you using perl)?!
    i would use regular expressions.
    if no characters are [A-Z] or [0-9], etc. would be false.

  4. #4
    chmod -x /bin/chmod
    Join Date
    Jan 2004
    Posts
    3,193
    Good morning Brian,

    okay, hmm, using case + regex, [0-9] would only pick up single digit?

    this what I got so far but it only picks up a single digit, and for some reason when I use something like [+-]?[0-9][0-9]? theres always a syntax error...

    I thought [+-] will allow for the first char to be either + or - or nothing at all because of the ? making it optional - same with [0-9]?

    hmm seems dodgy.

    Code:
            read numOne
    
            case $numOne in
            [0-9]) echo number;;
            *) echo invalid;;
            esac

  5. #5
    CC International Master Brian_Jones's Avatar
    Join Date
    Nov 2003
    Location
    Australia
    Posts
    2,058
    warn "has nondigits" if /\D/;
    warn "not a natural number" unless /^\d+$/; # rejects -3
    warn "not an integer" unless /^-?\d+$/; # rejects +3
    warn "not an integer" unless /^[+-]?\d+$/;
    warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2
    warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/;
    warn "not a C float"
    unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;

    ps. langer is out in the cricket

  6. #6
    chmod -x /bin/chmod
    Join Date
    Jan 2004
    Posts
    3,193
    Quote Originally Posted by Brian_Jones
    warn "has nondigits" if /\D/;
    warn "not a natural number" unless /^\d+$/; # rejects -3
    warn "not an integer" unless /^-?\d+$/; # rejects +3
    warn "not an integer" unless /^[+-]?\d+$/;
    warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2
    warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/;
    warn "not a C float"
    unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;

    ps. langer is out in the cricket
    Wow, did you write that on your own?

  7. #7
    chmod -x /bin/chmod
    Join Date
    Jan 2004
    Posts
    3,193
    How do I match up that regex against a variable (in bash script); case doesn't seem to accept the advanced regex

  8. #8
    chmod -x /bin/chmod
    Join Date
    Jan 2004
    Posts
    3,193
    I got this working thanks to Barry. Used tmpChar=${numOne:$tmpCounter:1}, while loop and ${#numOne} And then can match using regex as Brian suggested. Thanks Barry and Brian!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •