2013-03-08 23 views
0

我有一個解析bash中的xml的腳本。我遇到的要素之一的問題,因爲它包含了它裏面的一些命令字符串: 即:Bash腳本:使用命令解析字符串而不執行它

/usr/bin/printf "%b" "***** MONITORING ***** \n\n Notification Type: Critical \n Host: &{INTERNETMANAGED_URL00.URL} \n Estado: &{INTERNETMANAGED_URL00.Status} \n Time: &{INTERNETMANAGED_URL00.Status_Timestamp} Format: cyymmddhhmmssttt \n Contact to:APPSRV "|/usr/bin/mail [email protected] -c [email protected] -r "AAAAA" -s "** MONITORING - Server URL -Critical **" 

我怎麼能設置字符串轉換成變量(並打印)執行內部消除了裏面的命令?我最終在輸出中獲得了printf。

謝謝!

的代碼如下所示:

while read line 
do 

    if [[ $line == *"SITNAME"* ]] || [[ $line == *"TEXT"* ]] ; then 
     flag=true 
    fi 


    if [ $flag= 'true' ]; then 
     echo "$line" 
     writeLine=$writeLine';'"$line"    
    fi 


done < file.text 

echo $writeLine >> report.out 
writeLine="" 
flag=false 

輸出對回波

<![CDATA[/usr/bin/printf "%b" "***** MONITORING ***** nn Notification Type: Critical n Host: &{INTERNETMANAGED_URL00.URL} n Stat: &{INTERNETMANAGED_URL00.Status} n Time: &{INTERNETMANAGED_URL00.Status_Timestamp} Format: cyymmddhhmmssttt n Contact to:APPSRV "|/usr/bin/mail [email protected] -r "MONITORING" -s "** MONITORING - Server URL -Critical **"]]> 

輸出上的文件

<![CDATA[/usr/bin/printf "%b" "***** MONITORING file1.txt readme.txt file.xml script.sh nn Notification Type: Critical n Host: &{INTERNETMANAGED_URL00.URL} n Stat: &{INTERNETMANAGED_URL00.Status} n Time: &{INTERNETMANAGED_URL00.Status_Timestamp} Format: cyymmddhhmmssttt n Contact to:APPSRV "|/usr/bin/mail [email protected] -r "MONITORING" -s "** MONITORING - Server URL -Critical **"]]> 
+0

你的bash代碼是什麼樣的?通常,只讀文本並將其分配給變量不應該執行任何命令。 – chepner 2013-03-08 14:01:02

+0

我用一些代碼編輯了問題。謝謝! – TheMadCapLaughs27 2013-03-08 14:12:31

+0

該代碼中的任何內容都不會執行從'file.txt'中讀取的命令。是否僅包含包含「SITNAME」或「TEXT」的行或包含「SITNAME」或「TEXT」的行之後的每行? – chepner 2013-03-08 14:20:59

回答

1

這工作:

的WriteLine =「$ writeLine「';'」$ line「

...

回聲 「$的WriteLine」 >> report.out

感謝所有爲您的時間!

+0

問題在於,由於您沒有引用'$ writeline',所以星號正在作爲glob處理,並被擴展爲當前目錄中的文件。 – chepner 2013-03-08 16:37:40

+0

不需要在'writeLine =「$ writeLine」';'$ line「'中引用'$ writeLine'或'$ line',因爲路徑名擴展不是在變量賦值中執行的。然而,由於必須引用';',賦值可以簡化爲'writeLine = $ writeLine「;」$ line「或」writeLine =「$ writeLine; $ line」'。 – Armali 2014-06-05 11:09:52