autoescape
autoescape
Is there a program that will automatically escape characters in a text stream that would otherwise choke bash (like the function in PHP)
A program that would take input from stdin and escape all the special characters and spit it out again, I should probably knock up a script for it.
eg.
Say I have a text file called crap with rg46r3yj^ *hdfd3435( in it.
I could doto give me, rg46r3yj\^\ \*hdfd3435\(
eg.
Say I have a text file called crap with rg46r3yj^ *hdfd3435( in it.
I could do
Code: Select all
cat crap | autoescape
Well, you can strip the non-printable chars with "strings" but you just want them escaped, not sure why, but I'll go along with you on this. I assume you mean sort of like "od -c" but without the formatting? I don't know of one off the top of my head but it would be *extremely* easy to write one in C. Let's see what we can whip up in the next 5 minutes.
EDIT: I'm not sure what you mean by "special characters". I thought you meant "non-printable" chars but I see you have "^" and "*" which are printable. "special chars" in what context? Different chars are considered special depending on what you mean. I guess I need to see the bigger picture of what you are getting at. I hate to get bits and pieces only to work out a very complex solution for something that was very simple in the first place. :)
EDIT: I'm not sure what you mean by "special characters". I thought you meant "non-printable" chars but I see you have "^" and "*" which are printable. "special chars" in what context? Different chars are considered special depending on what you mean. I guess I need to see the bigger picture of what you are getting at. I hate to get bits and pieces only to work out a very complex solution for something that was very simple in the first place. :)
I meant the bash special characters.Void Main wrote:EDIT: I'm not sure what you mean by "special characters". I thought you meant "non-printable" chars but I see you have "^" and "*" which are printable. "special chars" in what context? Different chars are considered special depending on what you mean. I guess I need to see the bigger picture of what you are getting at.
The funny thing is, I have no idea why I wan't do this - you just made me consider it.I hate to get bits and pieces only to work out a very complex solution for something that was very simple in the first place.
We can forget about it if you like because I have no actual requirement for it, it just popped into my head sometime. Maybe i'm crazy

This only reads from stdin (has to have stuff piped to it or type in it directly):
escape.c
or shorter:
$ make escape
escape.c
Code: Select all
#include <stdio.h>
int main() {
char c;
while ((c = getchar ()) != EOF) {
switch (c) {
case '!' : case '@' : case '#' : case '$' : case '%' : case '^' : case '&' :
case '*' : case '(' : case ')' : case '-' : case '_' : case '=' : case '+' :
case '\\': case '|' : case '`' : case '~' : case '\'': case '\"': case '/' :
case '?' : case '.' : case '>' : case ',' : case '<' :
printf("\\%c",c);
break;
default: printf("%c",c);
}
}
}
Code: Select all
#include <stdio.h>
int main() {
char c;
char str[] = "!@#$%^&*()-_=+|`~""/?.>,<";
while ((c = getchar ()) != EOF) {
if (strchr(str,c)) printf("\\%c",c);
else printf("%c",c);
}
}
Of course you could also use "sed" or probably a few other standard UNIX utils:
Pipe your text through the following command:
e.g.
Pipe your text through the following command:
Code: Select all
sed 's/\([!@#$%^&*()_=+|`~"/?.>,<]\)/\\\1/g'
Code: Select all
$ echo "Hmm... &This might work! Maybe?" | sed 's/\([!@#$%^&*()_=+|\`~"/?.>,<]\)/\\\1/g'
Hmm\.\.\. \&This might work\! Maybe\?