Link to home
Start Free TrialLog in
Avatar of aacosta
aacosta

asked on

Passing blank spaces, |, % to my CGI in sh

Hi,
  When I pass blank spaces,%, |, etc
from my web page to my cgi it comes with
extra characters, do you know how to
avoid them?. I am using sh in UNIX (I can not
use Perl).

Thanks.
Avatar of ozo
ozo
Flag of United States of America image

What do you mean extra characters?
Do they show up as +, %25, %72, respectively?
Do you want to translate them back to space, %, | ?
Can you use awk?
Avatar of aacosta
aacosta

ASKER

yes,
Thats is what I want.
In this moment, the script shows me + as blank spaces, %25, %72, etc.

I need to translate this kind of characters to "normal" characters and so on (I think there are more than three "special characters")

Yes, I can use awk.

Thanks!.
Avatar of aacosta

ASKER

Adjusted points to 216
Avatar of aacosta

ASKER

It is urgent!.
I can use awk and almost any command of the shell (sh).
I need to receive a form (four lines) correctly in my cgi script.

I have just adjusted the points to 516!!.


nawk 'BEGIN{FS="%";ORS=""}{print $1; for( i=2; i<=NF; i++ ){printf "%c",system("ksh \"exit $((16#" substr($i,0,2) "))\""); print substr($i,3); } print "\n"}'
Avatar of aacosta

ASKER

Hi,
 I tried to use the statement you gave me but it did not worked.
The string that comes from my web page is called USERNAME and contains the following (this is an example, of course it changes for any character):

USERNAME=ale+acosta+alamo&OLD_PASSWORD=123456&NEW_PASSWORD=abc&CONF_NEW_PASSWORD=abc

(this is the value I obtain from echo $USERNAME in my cgi).

I used $1 and $USERNAME in the command but did not work either.

Thanks.
echo $USERNAME | tr '+' ' ' | nawk 'BEGIN{FS="%";ORS=""}{print $1; for( i=2; i<=NF; i++ ){printf "%c%s",system("ksh exit $((16#" substr($i,0,2) "))"),substr($i,3); } print RS }'
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aacosta

ASKER

It almost worked!. The blank spaces pass correctly, if I pass a variable with %, |, ?,;, etc there appear just white square (). Why?

Thanks again.

Ale,-
Avatar of aacosta

ASKER

Great! and thanks. I was trying the firts one.

Ale,-
Avatar of aacosta

ASKER

Thanks.
Avatar of aacosta

ASKER

Hi again, is there a way to substitude nawk by awk?, I tried to do it and did not work. In addition I tried gawk.

The problem is that I am now in a Linux box.

Sorry again.

Alejandro
it would be easiest if you could use Perl...
Avatar of aacosta

ASKER

Yes I know.
I would like to use perl but the code is already done in a shell script and now we want to adapt it to the web(sorry).
Can I substitude the nawk in your command with awk some way?. I tried to do it but did not work.

Thks!.-
shell scripts can call perl as easily as they can call awk or nawk.
It can still be done with awk, but it gets a bit messy:

awk 'BEGIN{FS="%";}{print "echo -n " $1; for( i=2;i<=NF; i++ ){print "echo $((16#" substr($i,0,2) ")) | awk \\{printf\\\"%c\\\",\\$1\\}"; print "echo -n " substr($i,3);} }' | ksh
Avatar of aacosta

ASKER

Hi, again.
I tried the command you told me and did not work.

In addition I tried:

echo $USERNAME|awk 'BEGIN{FS="%";}{print "echo -n " $1; for( i=2;i<=NF; i++ ){print "echo $((16#" substr($i,0,2) ")) | awk \\{printf\\\"%c\\\",\\$1\\}"; print "echo -n " substr($i,3);} }' | ksh

And did not work :-(

In addition I tried something like:

echo $USERNAME | tr '+' ' ' | awk 'BEGIN{FS="%";}{print "echo -n " $1; for( i=2;i<=NF; i++ ){print "echo $((16#" substr($i,0,2) ")) | awk \\{printf\\\"%c\\\",\\$1\\}"; print "echo -n " substr($i,3);} }' | ksh

And did not work neither.

Where do I have to use $USERNAME (the var from the web page) in your command (remember I am newbie).

If you like we can call perl from the shell script but I dont want to create a new file.

Sorry and thanks again.
I'd ask what you get from just
echo $USERNAME | tr '+' ' ' | awk 'BEGIN{FS="%";}{print "echo -n " $1; for(i=2;i<=NF; i++ ){print "echo $((16#" substr($i,0,2) ")) | awk \\{printf\\\"%c\\\",\\$1\\}"; print "echo -n " substr($i,3);} }'
without the | ksh
but if you can call perl from the shell script, it may be easier to just use
echo $USERNAME | perl -pe 'tr/+/ /;s/%(\w\w)/pack"H2",$1/ge'
Avatar of aacosta

ASKER

Hi, The first command did not work, my vars were empty.

The second one (with perl) worked perfectly!.

Really thanks again.

Alejandro Acosta A.