shell --- while
1
0
Entering edit mode
3.1 years ago
82101202089 ▴ 10

when I do something like this,I got a question:

yi@yi:~$ cat ids.txt
CDS
chromosome
exon
five_prime_UTR
gene
mRNA
scaffold
three_prime_UTR

# this way has a result
yi@yi:~$ while read line; do arr1[++i]=$line;done < ids.txt
yi@yi:~$ echo ${arr1[@]}
CDS chromosome exon five_prime_UTR gene mRNA scaffold three_prime_UTR

# but this one -- nothing!!!
yi@yi:~$ cat ids.txt | while read line;do arr2[++i]=$line;done
yi@yi:~$ echo ${arr2[@]}

yi@yi:~$

why!!!!

shell • 767 views
ADD COMMENT
1
Entering edit mode
3.1 years ago
Sam ★ 4.8k

This should answer you question:

https://stackoverflow.com/questions/4642191/read-line-by-line-in-bash-script

Essentially, when you do piping |, you want read CMD instead of read line

ADD COMMENT

Login before adding your answer.

Traffic: 1755 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6