Bourne Shell: Special characters Input Prevention


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bourne Shell: Special characters Input Prevention
# 1  
Old 04-14-2008
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  
Old 04-14-2008
Quote:
Originally Posted by totziens

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  
Old 04-14-2008
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  
Old 04-14-2008
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  
Old 04-14-2008
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  
Old 04-14-2008
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  
Old 04-14-2008
How about just this sed statement.

Code:
sed -n -e "s+^[a-zA-Z0-9/_\-]*$+&+p"

Code:
[/tmp]$ echo 'ab!cada' | sed -n -e "s+^[a-zA-Z0-9/_\-]*$+&+p" 
[/tmp]$ echo 'ab21212cada' | sed -n -e "s+^[a-zA-Z0-9/_\-]*$+&+p"
ab21212cada
[/tmp]$ echo 'ab2121/2cada' | sed -n -e "s+^[a-zA-Z0-9/_\-]*$+&+p"
ab2121/2cada
[/tmp]$ echo 'ab2-_121/2cada' | sed -n -e "s+^[a-zA-Z0-9/_\-]*$+&+p"
ab2-_121/2cada

Not extensively tested.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delimit the fields of a input file which has special characters?

Hi All, I am a newbie to Shell scripting. I have a requirement to Delimit the file fields of a Input file having special characters and spaces with ";". Input File ---------------------------------- Server Port ---------------------------------- Local ... (5 Replies)
Discussion started by: Suganbabu
5 Replies

2. Shell Programming and Scripting

awk match shell variable that contains special characters?

How to match a shell variable that contains parenthesis (and other special characters like "!") file.txt contains: Charles Dickens Matthew Lewis (writer) name="Matthew Lewis (writer)"; awk -v na="$name" ' $0 ~ na' file.txt Ideally this would match $name in file.txt (in this... (3 Replies)
Discussion started by: Mid Ocean
3 Replies

3. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

4. Shell Programming and Scripting

Check input for lenght, special characters and letter case

I made menu script for users so they can run other script without going in shell just from menu. But i must control their input. These are criteria: Input must have 4 signs First two signs are always lower case letters Input shall not have some special signs just letters and numbers ... (1 Reply)
Discussion started by: waso
1 Replies

5. Shell Programming and Scripting

Replacing string with special characters in shell

Hi, I am trying to replace a string in shell but it is not working correctly. @xcom.file@ needs to be replaced with tb137 Plz help.Thx. Please use and tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. (4 Replies)
Discussion started by: manish72
4 Replies

6. Shell Programming and Scripting

Ommiting special characters as input - Help

Hey Everyone, I'm quite new to unix (hence the 0 posts!) and im trying to write a simple program that outputs what the user types in to the screen, as long as it is a letter. This part works fine, however, when a "\" is entered doesnt not display anything and moves to the next line. Is... (11 Replies)
Discussion started by: ultiron
11 Replies

7. Shell Programming and Scripting

Help in preserving special characters from input file

Hi Forum. I've tried to search online for a solution but I cannot seem to find one. Hopefully, someone here can help me out. I would appreciate it. Input file abc.txt: $InputFile_Borrower=CMTSLST\EDW_COMMERCIAL_MTGE_BORROWER_dat.lst... (14 Replies)
Discussion started by: pchang
14 Replies

8. UNIX for Dummies Questions & Answers

Need help to escape special characters in Korn shell script

Hi, I would like to display the following message from my shell (Korn) script Copy "old_file.txt" to "new_file.txt" My code looks as follows print "Copy "old_file.txt" to "new_file.txt"" However, when I execute the script, I get the following output Copy old_file.txt to... (6 Replies)
Discussion started by: rogers42
6 Replies

9. Shell Programming and Scripting

Accepting user input in Bourne shell and using sed

He guys. Basically I want to make a script that can add, delete and view stuff in a external file called config.txt. I can open it up in Joe but im not sure how to read in the user input or using commands automatically in joe to edit, save then quit. Problem area below: 1) echo "Add... (1 Reply)
Discussion started by: Pits
1 Replies

10. Shell Programming and Scripting

Can't stop shell interpreting special characters

I am struggling with the following sample code: array1=(a b c d) array2=(* * * *) print ${array1} print ${array2} It returns 'c' and the name of a file in the directory I'm in. I can't for the life of me work out how to prevent the shell interpreting the '*' and just get it to return... (2 Replies)
Discussion started by: Doug97
2 Replies
Login or Register to Ask a Question