Bourne Shell: Special characters Input Prevention


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bourne Shell: Special characters Input Prevention
# 29  
Old 04-22-2008
try using egrep -v

try using grep

echo adfdffdg | egrep -v "\\$| \*|\&"

u can all unwanted character to this
# 30  
Old 04-22-2008
That's not going to work (especially not without double-quoting the variable). echo does its own parsing, that's why ksh offers print instead. But if you're not on ksh, you don't have that. (The [[ ... ]] construct is also not available in Bourne-compatible syntax, although any POSIX shell should have it.)

Maybe try printf instead, although that, too has some parsing of backslashes and percent signs. This is why I've been suggesting case all along.

In the case statement, you need to double the backslash; *\* means "anything followed by a literal asterisk".

But as already stated, the backslash is actually interpreted by read already. If you have read -r then use that.

PS. Run your spell checker to see how to spell "precede". (^:

Last edited by era; 04-22-2008 at 05:56 AM.. Reason: Note on [[ ... ]] syntax too
# 31  
Old 04-22-2008
Quote:
Originally Posted by aju_kup
try using grep

echo adfdffdg | egrep -v "\\$| \*|\&"

u can all unwanted character to this

It does not seem to work for "\":

>echo "adfdffdg" | egrep -v "\"
egrep: syntax error
> echo "adfdffdg" | egrep -v "/\"
egrep: syntax error
# 32  
Old 04-22-2008
Era, I have to agree that your case statement is the one closest to solving the problem. It's 99% working now.

As for "\", I have incorporated double backslash as follows:

case $b in
*[!-_A-Za-z0-9/]*) echo "Error" >&2;;
/*) echo "Error preceeded by /" >&2;;
*/) echo "Error ended with /" >&2;;
*\\*) echo "Error Also" >&2;;
*) ;;
esac


Unfortunately, the problem remains. See the result below

Enter directory: 12\99 [Still unable to detect the backslash]
Enter directory: 67\\88 [I am not sure why when the input comes with double backslash, the first case statement can detect them]
Error
Enter directory: 562\\\888
Error
Enter directory: 112\67\99
# 33  
Old 04-22-2008
use as follow

echo "asd\\\\asdfsa" | egrep -v "\\\\"
# 34  
Old 04-22-2008
Just discovered something new to me. The value of b, c and INPUT are all the same. Initially I thought I could compare the value of INPUT and b for identifying the backslash. The "\" is automatically removed from the values of b & c. It's a headache now!!!

c=$b
INPUT=`echo $b | cut -d '\' -f1`
echo "INPUT is $INPUT"
echo "b is $b"
echo "c is $c"
if [ "$INPUT" = "$b" ]; then
echo "OK"
else
echo "KO"
fi

Output:

Enter directory: 133\rwtw
INPUT is 133rwtw
b is 133rwtw
c is 133rwtw
OK
# 35  
Old 04-22-2008
See above. The backslash is interpreted and removed already by read but you could see if your shell understands read -r

No offense, but this is the third or forth time I try to point this out.


For understanding the egrep output, you need to understand how the backslash and the single and double quotes are interpreted by the shell and by grep.

Without quoting, a single backslash quotes the next character. echo \ is a syntax error (or rather, escapes the newline after the backslash, and gives you a secondary prompt). echo \\ gives a backslash as the argument to echo, which might echo it, or might complain that a lone backslash is a syntax error. (I get the former case.) echo "\" is a syntax error (or rather, the shell waits for the closing double quote; echo "\"" passes a single double quote, inside double quotes; it needs to be escaped so the shell doesn't treat it as the closing double quote) and echo "\\" again passes a single backslash as the argument to echo.

egrep uses backslashes for escaping (like some versions of echo), so you get a second level of backslash interpretation. egrep '\' is a syntax error; to grep for a literal single backslash, use egrep '\\', or (like aju_kup suggested, in double quotes, egrep "\\\\" -- each pair of backslashes is reduced by the shell's backslash parsing to a single backslash, so egrep gets two backslashes -- the first one escapes the second, to search for a literal backslash).

Last edited by era; 04-22-2008 at 08:22 AM.. Reason: quoting and echo discussion
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