我試圖與包含名爲itemlist.txt
的文本文件的工作:如何使用多個循環並將輸出存儲到多個變量?
http://example.com/item-a
http://example.com/item-b
http://example.com/item-c
http://example.com/item-d
http://example.com/item-e
我試過的代碼許多不同的變化。有些會返回只是項目,但不是網址。我無法弄清楚如何正確分配$url
。這是最接近我達到預期的輸出。
#!/bin/bash
while read url; do
for item in $(sed "s/http:\/\/example.com\///g"); do
echo $item $url; done
done < itemlist.txt
所需的輸出是:
item-a http://example.com/item-a
item-b http://example.com/item-b
item-c http://example.com/item-c
item-d http://example.com/item-d
item-e http://example.com/item-e
但不是我越來越:
item-b http://example.com/item-a
item-c http://example.com/item-a
item-d http://example.com/item-a
item-e http://example.com/item-a
有人可以闡明如何正確地做到這一點一些輕?
++爲解釋;總的來說,我仍然建議通過shell循環來調用一個工具。 – mklement0
關於第二個想法:標題表明_storing結果在shell變量_中可能是一個要求,儘管目前尚不清楚這是否僅僅意味着結束打印結果;無論如何:如果您需要將每行結果存儲在_shell variables_中,請使用_this_答案;考慮awk解決方案,如果它只是stdout輸出很重要。 – mklement0
同意; shell具有一些基本的數據處理能力,但通常它只能用於支持shell的主要存在理由,即執行其他程序。數據處理本身應該用更合適的語言來完成,比如'awk'。 – chepner