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.
Example:
Pasted data:
What shows up on the screen:
Moderator's Comments:
edit by bakunin: Please use code tags next time for your code and data.
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:
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.
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.
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)
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 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)
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)
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)
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'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,
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 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)
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)