Sponsored Content
Full Discussion: while read issue
Top Forums Shell Programming and Scripting while read issue Post 302699905 by bakunin on Wednesday 12th of September 2012 02:14:51 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 12:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy