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:
with
Another questionable thing is:
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:
Further, which output of "echo" should go there? There is none, therefore the redirection is superfluous:
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:
Furthermore, you use a "break" to leave the loop once input is exhausted:
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:
But again, the loop does nothing and a
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)