Operating System - Linux
1752687 Members
5402 Online
108789 Solutions
New Discussion юеВ

Re: '\' sripped in when reading a 'line'

 
Bolek Mynarski
Frequent Advisor

'\' sripped in when reading a 'line'

I came across a "pesky" situation where '\' is stripped when running a file through a 'while' loop and displaying content via either 'echo' or 'print' internal shell calls.

The shell in question is HP-UX implementation of /usr/bin/ksh or 1989 Korn implementation.

The idea is very simple: dump crontab to a file and discard anything that I do not need.

For example:

while read LINE; do
case "$LINE" in
*find*) continue ;;
*) echo "$LINE" >> newfile ;;
esac
done < file

I replaced echo with 'print' and tried to use -r for 'raw', no help. Each time '\' was stripped. For example:

find . -type f -exec ls -l {} \;

became

find . -type f -exec ls -l {} ;

I finally ended up using 'sed' to do what I wanted, however, I would like to do everything in shell.

So, my question is, how can I preserve '\' slash when reading a variable through either 'echo' or 'print' internal shell calls?

P.S. printf did not help either.

Thanks.
It'snever too late to learn new things...
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor

Re: '\' sripped in when reading a 'line'

The read isn't killing. print -r will not convert the escape sequences but you do need to carefully watch your quoting.

XX="AAA\tBBB"
print -r "${XX}"

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: '\' sripped in when reading a 'line'

Oh, and should add that just as there is a -r option for print there is also an equivalent -r option for read.

XX="AAA\tBBB"
print -r "${XX}" > /var/tmp/myfile

od -c /var/tmp/myfile # to convince yourself it's there

read -r YY < /var/tmp/mfile
print -r "${YY}" # backslash should be intact
If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: '\' sripped in when reading a 'line'

In the shell for my Linux test system I need to use -e to make it interpret the escape, -E to stop it.

'man ksh' gives on interesting description:

"echo arg ...
When the first arg does not begin with a -, and none of the arguments contain a \, then echo prints each of its arguments separated by a space and terminated by a new-line.
Otherwise, the behavior of echo is system dependent and print or printf described below should be used."

'man echo' suggest the -n as replied before:

"Notes
Berkeley echo differs from this implementation. The former does not implement the backslash escapes. However, the semantics of the \c escape can be obtained by using the -n option. The echo command implemented as a built-in function of csh follows the Berkeley semantics (see csh(1))."

fwiw,
Hein.
Arturo Galbiati
Esteemed Contributor

Re: '\' sripped in when reading a 'line'

Hi,
on my HP-UX 11i this runs fine:

while read -r LINE; do
case "$LINE" in
*find*) continue ;;
*) echo "$LINE" >> newfile ;;
esac
done < file

from man read:
Options and Arguments
read recognizes the following options and command-line arguments:

-r Do not treat a backslash character in any special
way. Consider each backslash to be part of the
input line.


HTH,
Art