Operating System - Linux
1753384 Members
5640 Online
108792 Solutions
New Discussion юеВ

Re: Filtering for specific chars.

 
SOLVED
Go to solution
john guardian
Super Advisor

Filtering for specific chars.

Guys:

/bin/sh, shell script, asks for user input and reads user input. The current script allows the user to input ANY character for comments. These comments are then written to a file that gets built on the fly and saved as an XML file. Further, a "home grown" interpreter/file parsing utility takes this xml file and reads it into another application. Finally, this app EXPLODES if any of the following characters have been input, at the beginning, by the user....

*, @, #, $, % and &.

Does anyone out there have a good method for looking at the user input string to verify whether or not it contains the above forbidden characters?

For example, the user might input a string like:

The upper & lower limits are out-of-range.

The character "&" is the problem. I'd like to be able to simply loop back, clear the string, give the user an error message flagging the illegal character and have them resubmit the comment with only alpha chars. I don't need any looping, etc stuff, just a working method to sense the illegal chars.

Any ideas? Thanks.
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: Filtering for specific chars.

Hi John:

This shell script shows one way:

cat .myfilter
#!/usr/bin/sh
while true
do
read LINE
echo ${LINE}|grep -q -e '\*' -e '\@' -e '\#' -e '\$' -e '\%' -e '\&'
[ $? = 0 ] && echo "REJECTED!" || echo "ok"
done

...run as ./myfilter

...and type sample lines with/without the characters you want to filter.

Regards!

...JRF...
Court Campbell
Honored Contributor

Re: Filtering for specific chars.

grep -cE '\@|\*|\#|\$|\%|\&'

will return a 1 if one of the characters is found and 0 if not.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
john guardian
Super Advisor

Re: Filtering for specific chars.

Guys, the above grep statements work great for everything "except" the '*' char. Instead of interpreting it as it does all of the other chars, it instead interprets the '*' as the list of all files and/or dirs in the current directory.

That's the problem that I ran into... which is why I asked for other solutions.

I appreciate the input... any other suggestions would also be appreciated.

Thanks.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Filtering for specific chars.

Hi (again) JOhn:

Ooops, a bit careless, I was:

Quote the variable as:

# echo "${LINE}"|grep -q -e '\*' -e '\@' -e '\#' -e '\$' -e '\%' -e '\&'

# cat .myfilter
#!/usr/bin/sh
while true
do
read LINE
echo "${LINE}"|grep -q -e '\*' -e '\@' -e '\#' -e '\$' -e '\%' -e '\&'
[ $? = 0 ] && echo "REJECTED!" || echo "ok"
done

Regards!

...JRF...
Patrick Wallek
Honored Contributor

Re: Filtering for specific chars.

In the shebang line of your script add a '-f' flag to sh so that it looks like:

#!/usr/bin/sh -f

The '-f' turns OFF file name generation, which is what the '*' is doing.
Dennis Handly
Acclaimed Contributor

Re: Filtering for specific chars.

>JRF: -e '\*' -e '\@' -e '\#' -e '\$' -e '\%' -e '\&'

Any reason you are using both types of quoting?
It would seem that you could just use fgrep so you wouldn't have to use \*. And the rest aren't special regexp(5) chars.
Bill Hassell
Honored Contributor

Re: Filtering for specific chars.

This may not be the complete list of special characters...indeed, what happens when the user types CTRL-G or some other special character? Another way to find special characters on a line is to use tr and character classes to remove the acceptable characters and if anything is left, the result is a list of unacceptable characters. The classes of characters are:

alnum, alpha, blank, cntrl, digit, graph, lower, print, punct, space, upper, xdigit

Use something like this:

### more code ###
read LINE
BADCHARS=$(echo "$LINE" | tr -d '[:alnum:]')
if [ ${#BADCHARS} -gt 0 ]
then
VIEWBAD=$(echo "$BADCHARS" | cat -v)
echo "Unacceptable characters: $VIEWBAD"
### more error handling ###
else
echo OK
fi

You can change the character class to [:alpha:] to eliminate numbers or other character classes may be combined with subsequent tr commands.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: Filtering for specific chars.

Hi (again):

Dennis wrote:

> JRF: -e '\*' -e '\@' -e '\#' -e '\$' -e '\%' -e '\&'

Any reason you are using both types of quoting?

Yes, although for other than '\$' both types of quouting were over-kill and I used them only for consistency.

What I was avoiding was matching any newline as:

# echo "\n"|grep -q \$ && echo NOT_WANTED!

Regards!

...JRF...
john guardian
Super Advisor

Re: Filtering for specific chars.

Thanks, again!