2011-03-22 83 views
0

我寫了一個從數據庫中獲取數據的腳本。在shell腳本中將字符串轉換爲xml?

現在我想增強該腳本來創建一個xml文件與數據庫返回的數據。

如何將該字符串轉換爲shell腳本中的xml文件。

script.sh

#!/bin/sh 
echo 'SELECT TABLE_NAME AS "Table Name", table_rows AS "Quant of Rows", ROUND((data_length + index_length)/1024/1024,2) AS "Total Size Mb" FROM information_schema.TABLES WHERE information_schema.TABLES.table_schema="database_name";' | mysql -u root -pmysql 

數據從數據庫

"Table Name\tQuant of Rows\tTotal Size Mb\ntable_1\t2\t0.02\ntable_2\t1\t0.02\ntable_3\t142\t0.02\ntable_4\t50839\t5.03\ntable_5\t16573\t5.13\ndlr\t0\t0.02\ntable_6\t6\t0.02\ntable_7\t0\t0.03\ntable_8\t2\t0.08\ntable_9\t4\t0.02\n" 

我想寫一些邏輯到腳本,這個字符串轉換爲XML文件中獲取。

我該怎麼做?處理這種情況的首選方法是什麼?

+0

請不要[交叉後](http://serverfault.com/questions/250290/convert-string-to-xml-in-shell-script)。 – 2011-03-23 02:50:40

回答

0
#!/bin/sh 
mysql -u root -pmysql --xml > /home/test/Desktop/temp.xml << eof 
SELECT TABLE_NAME AS "Table Name", table_rows AS "Quant of Rows", ROUND((data_length + index_length)/1024/1024,2) AS "Total Size Mb" FROM information_schema.TABLES WHERE information_schema.TABLES.table_schema='database_name'; 
eof