If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Validating user input

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-08-04, 09:40
sachin_mt sachin_mt is offline
Registered User
 
Join Date: Jan 2003
Posts: 106
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
Reply With Quote
  #2 (permalink)  
Old 04-08-04, 09:56
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #3 (permalink)  
Old 04-16-04, 07:59
sachin_mt sachin_mt is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 04-16-04, 08:51
praveenpr praveenpr is offline
Registered User
 
Join Date: Jan 2004
Location: Singapore
Posts: 89
Re: 2 questions

Quote:
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
Reply With Quote
  #5 (permalink)  
Old 04-16-04, 10:27
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On