The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-14-2008
totziens totziens is offline
Registered User
  
 

Join Date: Apr 2008
Location: Earth
Posts: 42
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.
  #2 (permalink)  
Old 04-14-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557
Quote:
Originally Posted by totziens View Post

I believe there is a better way to code than listing everything in a case statement.

Hope someone can suggest a better solution. Thanks.
if you are checking for the path that exists

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..
  #3 (permalink)  
Old 04-14-2008
totziens totziens is offline
Registered User
  
 

Join Date: Apr 2008
Location: Earth
Posts: 42
No, I am not checking for valid path.

In my script, the user is required to key in a path. The path input by the user will be created.

My problem is in the validation of the path input by user.
  #4 (permalink)  
Old 04-14-2008
totziens totziens is offline
Registered User
  
 

Join Date: Apr 2008
Location: Earth
Posts: 42
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
  #5 (permalink)  
Old 04-14-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Why dont you reverse your logic ? Look for the characters you need. Anything else will result in failure. You will have to enable the -noglob flag.
  #6 (permalink)  
Old 04-14-2008
totziens totziens is offline
Registered User
  
 

Join Date: Apr 2008
Location: Earth
Posts: 42
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
  #7 (permalink)  
Old 04-14-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Quote:
Originally Posted by totziens View Post
Only "/", "-", "_", alphabet and numeric are the valid characters.
Then search for anything containing a character which is not any of those.


Code:
case $b in *[!/-_A-Za-z0-9]*) echo error >&2;; esac

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0