LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   KORN: Looping through output of command (https://www.linuxquestions.org/questions/programming-9/korn-looping-through-output-of-command-470145/)

eur0dad 08-02-2006 01:03 PM

KORN: Looping through output of command
 
In bash, I normally use this to read each line of output:

while read line
do
echo $line
done < <(cat /myfile.txt | cut -f1 -d" ")

But this doesn't fly in KSH. I can't find the same thing on any tutorials, doesn't anyone know how to do this in Korn?

jim mcnamara 08-02-2006 02:37 PM

Code:

#!/bin/ksh
cat /myfile.txt | cut -f1 -d" " | \
while read line
do
echo $line
done


jlliagre 08-02-2006 03:16 PM

or with a closer syntax:
Code:

#!/bin/ksh
while read line
do
echo $line
done <<%
$(cat /etc/hosts | cut -f1 -d" ")
%


eur0dad 08-03-2006 11:15 AM

Quote:

Originally Posted by jim mcnamara
Code:

#!/bin/ksh
cat /myfile.txt | cut -f1 -d" " | \
while read line
do
echo $line
done



This method works perfectly. The other one that was posted kept giving me "% unclosed" errors.

jlliagre 08-03-2006 02:57 PM

Quote:

Originally Posted by eur0dad
The other one that was posted kept giving me "% unclosed" errors.

Huh ?

Please double check, it works for me with both the real ksh and pdksh.

You probably missed to copy the last line: "%"


All times are GMT -5. The time now is 03:37 PM.