bash shell腳本傳輸變數值空間處理,如何空間資料呢
**#FileList. list.txt Delivery list belowt**
$1 $2 $3
xxxx1.com_1 Hello_2 Hello_3 - Hello
xxxx2.com_1 Hello_2 Hello_3 - Hello
get_list() {
now_list=${1} #Pass list
while read -r line; do #Circular reading
now_list_url=$(echo "${line}"|awk -F ' ' '{print $1}') #url variable1
now_list_keyword=$(echo "${line}"|awk -F ' ' '{print $2}') #keyword variable2
now_list_title=$(echo "${line}"|awk -F ' ' '{print $3}') #title variable3
#print
echo "url:${now_list_url}"
result:xxxx1.com_1
echo "keyword:${now_list_keyword}"
result:Hello_2
echo "title:${now_list_title}"
result:Hello_3 #Due to the empty grid of the transmission variable 3
result:Hello_3 - Hello #And want to be correct
done < "${now_list}"
}
#Run
get_list list.txt`
傳輸變數 3 由于空間錯誤:Hello_3 我想要正確傳輸變數 3 由于空間錯誤,最后得到正確結果:Hello_3 - Hello
#而這里是因為傳輸變數不完整,所以我最終需要的結果是完整變數3的值中輸出完整變數3的值 = "Hello_3 - hello" 因為作業需要,列了很多處理,我將變數值保存在文本中我應該如何處理
謝謝
uj5u.com熱心網友回復:
如果我了解您要執行的操作,則應該read拆分欄位,而不是使用awk:
while read -r now_list_url now_list_keyword now_list_title; do
echo "url:${now_list_url}"
# result: xxxx1.com_1
echo "keyword:${now_list_keyword}"
# result: Hello_2
echo "title:${now_list_title}"
# result: Hello_3 - Hello
done < "${now_list}"
由于給了三個變數read,它會嘗試將行分成三個欄位。當行中有超過三個空格分隔的東西時,所有額外的東西都放在最后一個變數中。請參閱BashFAQ #1:“如何逐行(和/或逐欄位)讀取檔案(資料流、變數)?” 了解更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/529120.html
標籤:linux重击壳豆壳
