Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2003
    Posts
    106

    Unanswered: Validating user input

    Hi,

    I have a shell program which reads user input

    read a

    now i need to make sure that the variable a will not have special characters like single quote ' or double quote " how can I check this?

    can I validate the variable once it is read in a shell program.
    Sachi

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    You can do something like this :
    Code:
    # With 'if' and 'expr'
    
    if expr "$a" : ".*['\"].*" >/dev/null
       then echo "Variable contains special(s) character(s)"
       else echo "Variable is valid"
    fi
    
    # With 'case'
    
    case "$a" in
       *[\\'\"]*) echo "Variable contains special(s) character(s)";;
       *)          echo "Variable is valid" ;;
    esac
    Jean-Pierre.

  3. #3
    Join Date
    Jan 2003
    Posts
    106

    2 questions

    Hi,

    I have 2 questions

    1.
    In this line can you pl. explain why we need this >/dev/null

    if expr "$a" : ".*['\"].*" >/dev/null

    2.Is it possible to validate more than one variables say $a and $b in a single, if statament
    Sachi

  4. #4
    Join Date
    Jan 2004
    Location
    Singapore
    Posts
    89

    Re: 2 questions

    Originally posted by sachin_mt
    Hi,

    I have 2 questions

    1.
    In this line can you pl. explain why we need this >/dev/null

    if expr "$a" : ".*['\"].*" >/dev/null

    2.Is it possible to validate more than one variables say $a and $b in a single, if statament
    /dev/null is used to discard the other messages
    Thanks and Regards,

    Praveen Pulikunnu

  5. #5
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    The command 'expr "$a" : ".*['\"].*"' display the length of the matching string. With >/dev/null the display is invisible.

    To check variables a and b :

    if expr "$a$b" : ".*['\"].*"
    Jean-Pierre.

Posting Permissions

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