while read issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while read issue
# 1  
Old 09-12-2012
while read issue

I'm using while read in a script to create a file but when I paste on the screen it echos out different than the data. The file is created correctly and the script does as it should. Just trying to resolve what's being show on the screen.

Code:
    while read FILES
        do
        echo $FILES >> $UPLOADDIR/DATA 2>>/dev/null
        if [ "$FILES" = "" ];then
          break
        fi
        done


Example:

Pasted data:
Code:
dir1/dir2/dir3/dir4/file1
dir1/dir2/dir3/dir4/file2
dir1/dir2/dir3/dir4/file3
dir1/dir2/dir3/dir4/file4
dir1/dir2/dir3/dir4/file5
dir1/dir2/dir3/dir4/file6
dir1/dir2/dir3/dir4/file7

What shows up on the screen:
Code:
dir1/dir2/dir3/dir4/file1
dir1/dir2/dir3/dir4/file2
dir1/dir2/dir3/dir4/file3
dir1/dir2/dir3/dir4/file4
dir1/dir2/dir3/dir4/file5
dir1/dir2/dir3/dir4/file6
dir1/dir2/dir3/dir4/file7
dir1/dir2/dir3/dir4/file2
dir1/dir2/dir3/dir4/file3
dir1/dir2/dir3/dir4/file4
dir1/dir2/dir3/dir4/file5
dir1/dir2/dir3/dir4/file6
dir1/dir2/dir3/dir4/file7

Moderator's Comments:
Mod Comment edit by bakunin: Please use code tags next time for your code and data.

Last edited by bakunin; 09-12-2012 at 02:47 PM..
# 2  
Old 09-12-2012
There seem to be some misunderstandings about how the shell works:

Quote:
Originally Posted by toor13
I'm using while read in a script to create a file
You haven't told us which shell and which OS you use. If it is bash, a "\n" at the end of your output is implied and your construction is just superfluous, in Korn shell you should use the "print" command instead of the "echo" and your construction would still be superfluous.

The only thing you read is a stream of filenames and you do absolutely nothing with it, just put it into another file. You could do that without using a while loop, yes? Replace:

Code:
<some-stream-of-filenames> | while read FILES
        do
        echo $FILES >> $UPLOADDIR/DATA 2>>/dev/null
        if [ "$FILES" = "" ];then
          break
        fi
done

with

Code:
<some-stream-of-filenames> >> $UPLOADDIR/DATA

Another questionable thing is:

Code:
echo $FILES >> $UPLOADDIR/DATA 2>>/dev/null

You don't need to append to "/dev/null", because it has no content that needs to preserved (in fact no content at all), therefore:

Code:
echo $FILES >> $UPLOADDIR/DATA 2>/dev/null

Further, which output of "echo" should go there? There is none, therefore the redirection is superfluous:

Code:
echo $FILES >> $UPLOADDIR/DATA

Another thing is that filenames can contain whitespace in Unix. Whitespace is the field delimiter in shell, which is why it is wise to protect strings which could contain whitespace from the interpretation by the shell. Therefore:

Code:
echo "$FILES" >> "$UPLOADDIR/DATA"


Furthermore, you use a "break" to leave the loop once input is exhausted:

Code:
        if [ "$FILES" = "" ];then
          break
        fi

This is completely unnecessary, because you use "while read ..." "read" will return "FALSE" if the input ends and therefore the loop will be left automatically.

Finally: You always append to your output file ("$UPLOADDIR/DATA"). If you run the script several times you will have the output of your stream several times in your file. If you want to have only the results from one run in your file you will have to empty it prior to the loop, like this:

Code:
cat /dev/null > "$UPLOADDIR/DATA"
<some-stream-of-filenames> | while read FILES
        do
        echo $FILES >> $UPLOADDIR/DATA 2>>/dev/null
        if [ "$FILES" = "" ];then
          break
        fi
done

But again, the loop does nothing and a

Code:
<some-stream-of-filenames> > "$UPLOADDIR/DATA"

would do the same. In case you want to process the filenames in the loop somehow and you use Korn shell you should do it this way:

Code:
exec 3> "$UPLOADDIR/DATA"       # to create a new empty file
# exec 3>> "$UPLOADDIR/DATA"    # alternatively to append to the file

<some-stream-of-filenames> | while read FILES ; do
     print -u3 "$FILES"
done

exec 3>&-

I hope this helps.

bakunin
# 3  
Old 09-12-2012
Quote:
Originally Posted by bakunin
There seem to be some misunderstandings about how the shell works:


You haven't told us which shell and which OS you use.

There is nothing in the script that is shell or OS specific, so the shell and the OS are both irrelevant.
Quote:
If it is bash, a "\n" at the end of your output is implied

In all shells, echo adds a newline.
Quote:
and your construction is just superfluous, in Korn shell you should use the "print" command instead of the "echo" and your construction would still be superfluous.

Neither echo nor print is a good idea.

Use printf; it is the only portable method.
# 4  
Old 09-13-2012
Quote:
Originally Posted by cfajohnson
There is nothing in the script that is shell or OS specific, so the shell and the OS are both irrelevant.
Across implementations in different OSes ksh might either be ksh88 or ksh93, which exhibit differences regarding the treatment of "echo". Furthermore, if "/bin/echo" is found in the PATH, ksh treats its own built-in differently, even if it is there. With bash the situation may or may not be that complicated, but i lack experience with all bash-implementations there may be, so i can't say for sure.

Therefore my opinion is that knowing the OS and the shell thread-opener talks about may or may not matter. I can only decide once i know these, not beforehand.

Quote:
In all shells, echo adds a newline.
In some shells "echo" is not a built-in but a call to "/bin/echo". If the implentation of each respective OS does or doesn't add a newline i don't know. I haven't used each and every Unix-like system there is.

Quote:
Neither echo nor print is a good idea.

Use printf; it is the only portable method.
With Korn shell "print" is a built-in and is preferred. (see here) With bash "echo" is a built-in and is probably preferred too (not so sure as i don't use bash more than i have to, but most bash scripts i have seen use "echo" for that purpose).

To ask for "portability" in general without specifying where you would want to port it is completely arbitrary: in general it is a good idea to write scripts in a way so that they work on as many systems as possible with as few changes as possible. To ask for a ksh script that it has to work in bash (or vice versa) makes about as much sense as asking for a FORTRAN program to compile on a C-compiler without error.

Again, i don't say portability should not be an issue. But "portability" as a goal makes only sense if there is a reference platform/shell/environment defined to which it should be portable. Without such a definition it is just a buzzword.

Probably you imply POSIX as this reference here and mean "portability towards the POSIX-specification", which is as good a goal as any else. If the thread-opener has POSIX-compatible systems and might face the necessity to port the script from one of these POSIX-compatible systems to another then you are right - on the other hand, if he has another set of systems he might be better off to enforce compatibility between the systems he actually has than towards a generic standard some of his systems do not adhere to.

Insofar it would also be a good idea to know more about his actual environment instead of declaring this "irrelevant" like above.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While read pipe input issue

Hello, I have an ffmpeg bash script which is working nice and I need to do the same for other sources. To create new scripts and to deal with multiple bash files sounds not logical. It is a bit hard to manage for me.. I wondered if it was possible to make my input file as variable. Then I... (1 Reply)
Discussion started by: baris35
1 Replies

2. Shell Programming and Scripting

File read format issue in UNIX

hi all. my loop is getting failed eventhoug it is 1=1 but it is failure message. any help plz Output expected : echo "sucesss" code out=`cat bit.txt` if ]; then echo "sucess" else echo "Failure" (2 Replies)
Discussion started by: arun888
2 Replies

3. Shell Programming and Scripting

ksh while read issue

Hello, I have used a chunk of ksh script similar to this in many places without any issue: while : do print; read OPTION?"Enter a number (q to quit): " expr ${OPTION} + 1 >/dev/null 2>&1 CHECKVAL=$? if }" != ${OPTION} ]; then ... (2 Replies)
Discussion started by: port43
2 Replies

4. Shell Programming and Scripting

Read line, issue with leading - and {}'s

Heyas With my forum search term 'issue with leading dash' i found 2 closed threads which sadly didnt help me. Also me was to eager to add the script, that i didnt properly test, and just now figured this issue. So i have this code: if ] then while read line do line="${line/-/'\-'}"... (7 Replies)
Discussion started by: sea
7 Replies

5. Shell Programming and Scripting

Issue in using read keyword twice

Hi, I have a situation where i need to read line by line from a text pad and with each line , i need to take inputs from command line and do some process. Using below code i am not able to use 'read' keyword twice. Can any one please help cat > t.txt a d c > cat > t.ksh while read... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

6. Shell Programming and Scripting

while read LINE issue

Hi, This is the script and the error I am receiving Can anyone please suggest ? For the exmaple below assume we are using vg01 #!/bin/ksh echo "##### Max Mount Count Fixer #####" echo "Please insert Volume Group name to check" read VG lvs |grep $VG | awk {'print $1'} > /tmp/audit.log ... (2 Replies)
Discussion started by: galuzan
2 Replies

7. UNIX for Advanced & Expert Users

read() from ttyS1 issue while write() is Ok

Hi! I've got a problem with reading from serial port, when I run this code on Digi ConnectCore Wi-9c. But writing to serial port is Ok. By the way, when I'm running this code on "full" Linux it is working Ok - I can read and write to serial without mistakes. Where is a problem? uname -a:... (3 Replies)
Discussion started by: Japonomatj
3 Replies

8. Shell Programming and Scripting

Multi Line 'While Read' command issue when using sh -c

Hi, I'm trying to run the following command using sh -c ie sh -c "while read EachLine do rm -f $EachLine ; done < file_list.lst;" It doesn't seem to do anything. When I run this at the command line, it does remove the files contained in the list so i know the command works ie... (4 Replies)
Discussion started by: chrispward
4 Replies

9. Shell Programming and Scripting

While loop read line Issue

Hi I am using while loop, below, to read lines from a very large file, around 400,000 rows. The script works fine until around line 300k but then starts giving incorrect result. I have tried running the script with a smaller data set and it works fine. I made sure to include the line where... (2 Replies)
Discussion started by: saurabhkumar198
2 Replies

10. UNIX for Dummies Questions & Answers

While read issue...

I think I saw another post where someone said he an issue with a 'while within a while' but there were no solutions on it. I have an input file of data columns separated by pipes "|" and I want to set each column to a variable and do something with it. This I can do. When I get to a certain... (5 Replies)
Discussion started by: giannicello
5 Replies
Login or Register to Ask a Question