我試圖寫一個bash腳本,列出我的本地的Postgres表DBwhile循環將返回一個額外的空行
while read line
do
myarray+=("$line")
## debug
echo $line
done < <(psql -h ... -U ... -t -c "select distinct table_name from information_schema.columns where table_schema='myschema' ;")
我已經創建了36桌,但上面的代碼輸出第37行是完全空白。我檢查了像echo ${#myarray[@]}
這樣的元素的數量; 37個元素。
如果我運行查詢select distinct table_name from information_schema.columns where table_schema='myschema' ;
,我收到36條記錄。
我在while
循環中使用過程替換方法,因爲它看起來更快,但我認爲錯誤來自那裏。有任何想法嗎?
循環似乎是矯枉過正。難道你不能只說'myarray =($(psql ...))'? –
你檢查過'psql'的輸出嗎?它最後有兩個換行符還是\ r \ n?你可以發佈'psql ... |的輸出嗎? hd'(十六進制轉儲)? – ideaboxer
除非你希望在輸出中間有空行(並且你想保留它們),那麼你可以添加(warning:bashism)'[[「$ line」=〜^ [[:space:]] * $]] && continue'在循環的頂部。 –