i am using this
but this is only printing the strings that end with \n, and skipping the last one because it doesnt have it. How can I print them all?Code:#!/usr/bin/env bash curdir=`pwd` while read line; do printf $line done < "test" cd $curdir
i am using this
but this is only printing the strings that end with \n, and skipping the last one because it doesnt have it. How can I print them all?Code:#!/usr/bin/env bash curdir=`pwd` while read line; do printf $line done < "test" cd $curdir
I'm not catching the part where you tell the script what to read. It looks like it reads from the standard input.
Also, why in the world do you change to your current directory? (cd `pwd`)
According to my man page, 'read' can be given a fd, and will return on eof, so making it do what you want should be very possible with 'read'. Check out the page for your self, it could be of some help.
This forum is for C programming questions specific to Linux. You would be better off going to a Linux specific forum, but I do have something of an answer for you.
You need to check the return value of read, to see if it's zero (read a normal line, with a newline at the end) or non-zero (something else, as Yarin pointed out). The solution I came up with is:
Note, you can't specify "test" as you did with the quotes, since that's a string, not a file name, and < redirect doesn't work with strings. You would have to use echo "test" | .... Also, if some of the files you process do have a newline at the end of the file, you may need to deal with the empty line it reads since it saw the EOF. You can do something like if $line is not empty or $x is zero, print $line.Code:x=0 # used to track the return value of read while [ $x == 0 ]; do # bash has no do-while, this will stop only when x is non-zero, i.e. read gets an EOF read line # read a line, up to a \n x=$? # set x to the return value of the last command printf $line done < foo.txt
I'm not a bash expert, so there may be a way sexier way. Two bash references I like are Deadman.org: Advancing in the Bash Shell and BASH Cures Cancer.
The "sexy" way is how he's doing it. It should be working. I suspect the reason it's not working is because the last line has no newline, and though it DOES get printed, it doesn't flush to the screen right away. If you just put a blank "echo" right before the "cd $curdir" what happens?
Code:#!/usr/bin/env bash curdir=`pwd` while read line; do printf $line done < "test" echo cd $curdir
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Yeah, bash read it, but unfortunately, read got an EOF, so it skipped the while loop that would print it. Since the data isn't in the output buffer, an echo wouldn't flush anything. You're right he is doing it the better way, but the solution is not a plain echo, but to echo $line after the loop. I think this should take care of any file, whether or not it has the trailing end of line:
The echo -n doesn't print the trailing new line, so it's more like printf in that regard. I would just use printf, but it complains about an empty parameter list if $line is empty.Code:while read line; do printf $line done < "test" echo -n $line