![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help to escape special characters in Korn shell script | rogers42 | UNIX for Dummies Questions & Answers | 6 | 05-14-2009 08:23 AM |
| Accepting user input in Bourne shell and using sed | Pits | Shell Programming and Scripting | 1 | 09-09-2007 10:39 AM |
| Can't stop shell interpreting special characters | Doug97 | Shell Programming and Scripting | 2 | 11-23-2005 09:41 PM |
| special characters | nawnaw | UNIX for Dummies Questions & Answers | 2 | 05-18-2004 04:17 PM |
| awk/sed with special characters | apalex | Shell Programming and Scripting | 5 | 05-06-2002 05:40 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Bourne Shell: Special characters Input Prevention
Environment: Sun UNIX
Language: Bourne Shell Problem: I am writing a script that allows user to key in a directory. Example: /root/tmp. Since the user can key in anything he/she wants, I need to validate to make sure that he/she does not key in anything funny i.e. #@!*&^$><,."';:}[]+=)(|\%~` Only "/", "-", "_", alphabet and numeric are the valid characters. Initially, I wrote the code as follows but it does not work for certain special characters such as ")", "(", "\" and "`" (there could be more that I may have missed). Basically, it complained that the script had syntax error. case "$b" in *@*) echo "Error: It has @";; *~*) echo "Error: It has ~";; *#*) echo "Error: It has #";; *\**) echo "Error: It has *";; *) echo "Directory is ok";; esac I believe there is a better way to code than listing everything in a case statement. Hope someone can suggest a better solution. Thanks. |
|
||||
|
Quote:
Code:
if [ -e $path ] ;then # Pass else # Not valid path fi or Code:
echo $path | awk '/[<>@*]/' and check for status Last edited by ghostdog74; 04-14-2008 at 09:07 AM.. |
|
||||
|
Here's my more detailed script if it helps:
while : do echo "Please enter the directory: \c" read b if [ "$b" = "" ]; then echo "ERROR: Directory should not be blank!" else # Validation: more condition is required. This is an area I need help case "$b" in *@*) echo "Error: It has @";; *~*) echo "Error: It has ~";; *#*) echo "Error: It has #";; *\**) echo "Error: It has *";; *) echo "Directory is ok" break;; esac fi done |
|
||||
|
I have thought about it too but my skill in Bourne shell is limited and I could not figure out how I can do it. I have the following codes on my mind but I could not code something that is good enough to prevent the unwanted special characters from being entered by user:
# This will prevent user from entering /, _ & - which should be allowed echo $b | grep "[^0-9]" > /dev/null 2>&1 if [ "$?" -eq "0" ]; then echo "ERROR: Invalid Directory!" fi # This will prevent user from entering /, _ & - which should be allowed echo $b | grep "[^a-z]" > /dev/null 2>&1 if [ "$?" -eq "0" ]; then echo "ERROR: Invalid Directory!" fi # This will prevent user from entering /, _ & - which should be allowed echo $b | grep "[^A-Z]" > /dev/null 2>&1 if [ "$?" -eq "0" ]; then echo "ERROR: Invalid Directory!" fi |
|
||||
|
Quote:
Code:
case $b in *[!/-_A-Za-z0-9]*) echo error >&2;; esac |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|