YNWA
YNWA

Reputation: 700

how to redirect a file into "while read VAR" loop?

beginner here,

I have the following code

while read LINE < $1
do
LAST_LINE=$LINE
done

basically, I want to read the file in $1 and extract the last line but the redirection doesnt work, I've tried to use

while read < $1 LINE

aswell, doesnt work...

it still wants some input from the user

how to do this correctly?

thanks!

Upvotes: 0

Views: 290

Answers (2)

Emery Lapinski
Emery Lapinski

Reputation: 1662

cat $1 | while read LINE
do
LAST_LINE=$LINE
done

would be a lot more readable in my opinion

Upvotes: 0

cnicutar
cnicutar

Reputation: 182619

Do it at the end:

while read line
do
...
done < "$1"

You might also be interested in

LAST_LINE=$(tail -1 "$1")

Upvotes: 5

Related Questions