2014-03-13 24 views
2

爲什麼在執行下面的腳本時每個printf(用echo回覆)都打印在同一行?BASH中echo/printf中沒有換行while循環

function read_dom() { 
    local IFS=\> 
    read -d \< ENTITY CONTENT 
} 

cat my_xml_file.xml | \ 
{ while read_dom; do 
     printf "(entity:content %s:%s)" $ENTITY $CONTENT 
} 

現在,這產生了一個線路輸出:

(entity:content member:)(entity:content name:id)(entity:content /name:) 

如何更改爲多,如:

(entity:content member:) 
(entity:content name:id) 
(entity:content /name:) 
+0

'printf'不是'echo'。你需要'\ n'。 – devnull

+0

'printf「(entity:content%s:%s)\ n」$ ENTITY $ CONTENT' – devnull

回答

4

你只需要添加換行符,\n,至printf聲明:

printf "(entity:content %s:%s)\n" $ENTITY $CONTENT 
4

printf不追加換行符作爲標準的行爲,你需要把它添加到您的打印字符串:

printf "(entity:content %s:%s)\n" $ENTITY $CONTENT