Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2004
    Posts
    2

    Unanswered: Bourne scripting - checking user input

    Hi all,
    As part of a Bourne shell script I'm writing, I need to check the user's input to make sure that it meets requirements (in this case, only alphabetic characters).
    The way I'm doing it at the moment is reading the variable, then using a case statement to print an error if the input is incorrect.

    Code:
    read word
    case $word in
           [a-zA-Z]+) echo "Valid" ;;
           *) echo "Error" ;;
    esac
    I'm guessing that the case statement doesn't support the regular expression [a-zA-Z]+ , any suggestions on other ways I could go about it?
    Thanks.

  2. #2
    Join Date
    Jun 2004
    Posts
    29

    A bit awkward but...

    Code:
    read uname
    if [ `expr length $uname` -ne `expr "$uname" : [A-Za-z]` ]
    then
      echo "Nasty warning message"
      exit
    fi

  3. #3
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Apply some reverse logic!

    case $word in
    *[!a-zA-Z]*) echo contains nonalphas;;
    *)echo contains alphas only;;
    esac

  4. #4
    Join Date
    Aug 2004
    Posts
    2
    Thanks a lot Damian, seemed to do the trick.

  5. #5
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Quote Originally Posted by Chewy
    Thanks a lot Damian, seemed to do the trick.
    Chewy,

    The problem with your initial attempt was that your expression was a 'regular expression' - the shell is only understands 'file expressions', which are completely different!

Posting Permissions

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