Input value changing into 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Input value changing into 0
# 1  
Old 02-22-2013
Input value changing into 0

Hi,

I am getting a strange problem in my production environment.

Line in input file:
Code:
[ASD_HAGDF_CSAS_CMPPRD.s_PRDPR_STNNSTN_CSAS_SRCTPP_COMMHIST006_INC_dat_LD]

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

Output:
Code:
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

Last edited by Scrutinizer; 02-22-2013 at 06:24 PM.. Reason: code tags
# 2  
Old 02-22-2013
Quote:
Originally Posted by niba
while read myline
do
echo $myline >> /home/aauytrf/PARM_FILE_NM.bak
fi
done < /home/aauytrf/PARM_FILE_NM.prm
Why there is a fi statement in your while loop?

Also always wrap string variables in double quotes:
Code:
while read myline
do
    echo "$myline" >> /home/aauytrf/PARM_FILE_NM.bak
done < /home/aauytrf/PARM_FILE_NM.prm

# 3  
Old 02-22-2013
Thats a type there is no if statement. Code is

Code:
 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.
# 4  
Old 02-23-2013
Not sure I understand. What output is "0"? Do you have a line
Code:
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.
# 5  
Old 02-23-2013
OK I think I know what's up...

From the READ manual...

Code:
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...

Code:
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....
Code:
#!/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...

Code:
#!/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...
# 6  
Old 02-24-2013
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.
# 7  
Old 02-24-2013
What does
Code:
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:
Code:
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

2. UNIX for Dummies Questions & Answers

Getting input and changing variable?

Hi I am new to scripting and have a function in my .sh script file that outputs a html radio button form weather_forecast_config() { echo "" echo "<html><head><title>Welcome</title></head>" echo "<body>" echo "<h2>Weather Forecast - Change City</h2>" echo "<form name="input"... (5 Replies)
Discussion started by: scriptnewbie
5 Replies

3. Shell Programming and Scripting

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

4. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

5. Shell Programming and Scripting

Create Multiple files by reading a input file and changing the contents

Being new to this area .I have been assigned a task which i am unable to do . Can any one please help me . Hi I have requirement where i have input file XYZ_111_999_YYYYMMDD_1.TXT and with header and series of Numbers and Footer. I want to create a mutiple output files with each file having a... (2 Replies)
Discussion started by: bhargavkr
2 Replies

6. UNIX for Dummies Questions & Answers

send output of a file as input for changing date

Hi, Please help me out on this one. I want to send the output of a file as input for changing the date using date command. Example, i have a file date.txt whose contents are 081014462009 I need to use the date in that file as input for date command. I tried cat date.txt | date ; but it... (2 Replies)
Discussion started by: foxtron
2 Replies

7. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

8. Shell Programming and Scripting

Changing userID and Changing group and GID

Hello, I want to write a ksh script about changing UID and changing group with GID. There are multiple servers i want to perform that job. linux1 linux2 linux3 linux4 linux5 ...... . . . . . 1.) How can i enter "password" in script rather asking me? I was trying this... ssh... (2 Replies)
Discussion started by: deal732
2 Replies

9. Shell Programming and Scripting

How to prompt for input & accept input in ONE line

hi, am a new learner to shell programming. i have a script which will prompt for user to key in their name & display their name afterwards. script ===== echo "Pls enter your name:" read name echo "Your name is $name." output ===== Pls enter your name: Bob Your name is Bob. what... (2 Replies)
Discussion started by: newbie168
2 Replies

10. Programming

Changing stdin from file redirection to console input

Hi I am doing file redirection at console for use by my binary. %console%> bin &lt inputfile After reading in the entire file, I want my program to continue taking input from the console. So essentially I want to redirect stdin back to console. But I cant figure out how to do it. I am... (4 Replies)
Discussion started by: nauman
4 Replies
Login or Register to Ask a Question