我觀察到一種行爲,我並沒有失去理智,並希望有人對此有所瞭解。Bash:在循環中從STDIN中讀取的奇怪行爲
我有兩個腳本,都從STDIN讀取。
讀數從鍵盤的序列(1輸入2輸入3輸入...)
腳本A輸出 「X」 每次
#!/bin/bash
while read LINE
do
echo "x" # this happens everytime
echo $LINE # this is the only different line
done
output:
1
x
1
2
x
2
3
x
3
4
x
4
5
x
5
腳本B打印的 「x」 僅在第一次它讀LINE
#!/bin/bash
while read LINE
do
echo "x" # this happens only the first time
awk '{print $LINE}' # this is the only different line
done
output:
1
x
2
2
3
3
4
4
5
5
有人可以解釋這一點嗎?
偉大的答案,我發誓我發現這種棘手的情況,意外。這對初學者來說會是一個很好的考試問題,哈哈。 –