Special Characters in Input lines

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Special Characters in Input lines

Karl Godt
Administrator
Recently there was some trying to fix special characters in inputlines - for passwords .

Here's a function that converts Characters of a STRING into OCTALS :
_string_to_octal()
{
_debug "_string_to_octal:$*" >&2
unset oSTRING
if test "$*"; then
STRING_ORIG="$*"

STRING=`echo "$STRING_ORIG" | sed 's!\(.\)!"\1"\n!g'`
_debug "_string_to_octal:STRING='$STRING'" >&2


while read -r oneCHAR
do
oneCHAR=`echo "$oneCHAR" | sed 's!^"!!;s!"$!!'`
oCHAR=`printf %o \'"$oneCHAR"`

oSTRING=$oSTRING"\\0$oCHAR"

done<<EoI
`echo "$STRING"`
EoI

else

while read -r oneLINE
do
#test "$oneLINE" || continue
 _debug "oneLINE='$oneLINE'" >&2
 STRING=`echo "$oneLINE" | sed 's!\(.\)!"\1"\n!g'`
 _debugx "STRING='$STRING'" >&2
 while read -r oneCHAR
 do
 _debugx "oneCHAR='$oneCHAR'" >&2
 oneCHAR=`echo "$oneCHAR" | sed 's!^"!!;s!"$!!'`
 _debug "oneCHAR='$oneCHAR'" >&2
 oCHAR=`printf %o \'"$oneCHAR"`
 _debug "oCHAR='$oCHAR'" >&2
 #test "$oCHAR" = 134 && oCHAR=0134

 oSTRING=$oSTRING"\\0$oCHAR"
 _debugx "oSTRING='$oSTRING'" >&2

 done<<EoI
`echo "$STRING"`
EoI

oSTRING=$oSTRING"\\012"
done

fi

echo "$oSTRING"

}

Needs these functions :
DEBUG=1
_debug(){
test "$DEBUG" || return 0
echo "$0:DEBUG:$*"
}

DEBUGX=
_debugx(){
test "$DEBUGX" || return 0
echo "$0:DEBUGX:$@"
}

Can be used as
_string_to_octal "STRING"
and
echo "STRING" | _string_to_octal

Should work as _string_to_hex with printf %x
Loading...