Input value changing into 0

Hi,

I am getting a strange problem in my production environment.

Line in input file:

[ASD_HAGDF_CSAS_CMPPRD.s_PRDPR_STNNSTN_CSAS_SRCTPP_COMMHIST006_INC_dat_LD]

Code:

while read myline
do
echo $myline >> /home/aauytrf/PARM_FILE_NM.bak
fi
done < /home/aauytrf/PARM_FILE_NM.prm

Output:

0

Here the input line is becoming 0. This is not happening to all lines in the input file, only to some lines alone.

Strangest thing is I am unable to re-craete this issue in my test environment.

Can some one shed some light into what is going wrong here.

Regards
niba

Why there is a fi statement in your while loop?

Also always wrap string variables in double quotes:

while read myline
do
    echo "$myline" >> /home/aauytrf/PARM_FILE_NM.bak
done < /home/aauytrf/PARM_FILE_NM.prm

Thats a type there is no if statement. Code is

 while read myline
do
echo $myline >> /home/aauytrf/PARM_FILE_NM.bak
done < /home/aauytrf/PARM_FILE_NM.prm

One more point the script does not contain #! as the first line, so I am not sure whihc interpreter is used to execute. My script is called from another script which has #!/usr/bin/ksh, so I think it is korn shell.

Not sure I understand. What output is "0"? Do you have a line

0

in your .bak file? Pls post more meaningful input and output samples, and maybe an execution log of your script. From what I see in your post, there's no indication of sth. going wild.

From the READ manual...

If count is zero, read() returns zero and has  no  other  results.   If
       count is greater than SSIZE_MAX, the result is unspecified

Also I did a little test...

cat test.txt

[ASD_HAGDF_CSAS_CMPPRD.s_PRDPR_STNNSTN_CSAS_SRCTPP_COMMHIST006_INC_dat_LD]
[bla.bla]
 [bla.bla] [bla.bla]
\[bla.bla\]
No.Brackets.Bla.Bla
[NO_PERIODS]
[NO.UNDERSCORE]

my results using bash with the debug option....

#!/bin/bash
while read myline
do
echo $myline 
done < test.txt
+ read myline
+ echo a
a
+ read myline
+ echo a b
a b
+ read myline
+ echo a b a b
a b a b
+ read myline
+ echo a b
a b
+ read myline
+ echo No.Brackets.Bla.Bla
No.Brackets.Bla.Bla
+ read myline
+ echo '[NO_PERIODS]'
[NO_PERIODS]
+ read myline
+ echo '[NO.UNDERSCORE]'
[NO.UNDERSCORE]
+ read myline
+ echo

+ read myline

So I think what happens is that the ^[ or [bl la] is being interpreted as a type of non ASCII character. When I tranpose the brackets on either side of your original string...it shows up...

#!/bin/bash
while read myline
do
echo $myline 
done < test.txt
+ read myline
+ echo a
a
+ read myline
+ echo a b
a b
+ read myline
+ echo a b a b
a b a b
+ read myline
+ echo a b
a b
+ read myline
+ echo No.Brackets.Bla.Bla
No.Brackets.Bla.Bla
+ read myline
+ echo '[NO_PERIODS]'
[NO_PERIODS]
+ read myline
+ echo '[NO.UNDERSCORE]'
[NO.UNDERSCORE]
+ read myline
+ echo ']ASD_HAGDF_CSAS_CMPPRD.s_PRDPR_STNNSTN_CSAS_SRCTPP_COMMHIST006_INC_dat_LD['
]ASD_HAGDF_CSAS_CMPPRD.s_PRDPR_STNNSTN_CSAS_SRCTPP_COMMHIST006_INC_dat_LD[
+ read myline

I would use a different manner of backing up...

That quote is referring to the read C library function, not the shell's read command. Two different things.
I cannot reproduce your test. When running that code snippet on your test.txt file, lines are output as is except for some whitespace removal.

What does

while read myline
do
  printf "%s\n" "$myline"
done < /home/aauytrf/PARM_FILE_NM.prm >> /home/aauytrf/PARM_FILE_NM.bak

produce?

For a literal copy use:

while IFS= read -r myline
do
  printf "%s\n" "$myline"
done < /home/aauytrf/PARM_FILE_NM.prm >> /home/aauytrf/PARM_FILE_NM.bak

But why isn't cat being used instead?

Hi All,

The code snippet I pasted was only the portion of code whihc I had issue. There are more processing happening inside while loop, but none of those statments is responsible for input line being changed to "0" while doing doing echo. This is why I dint paste them. This is not just a backing up of file.

OK, and what did the printf produce?

 
printf

is producing same line as output. That is if input is
[ASD_HAGDF_CSAS_CMPPRD.s_PRDPR_STNNSTN_CSAS_SRCTPP_COMMHIST006_INC_dat_LD]
out put is same.

See post #4.

RudiC,

For your question "Not sure I understand. What output is "0"? Do you have a line " answer is "0" is what is the output from echo statement.
And there is no other output from logfile.