Bash脚本:使用命令解析字符串而不执行它

时间:2013-03-08 13:31:40

标签: linux bash shell

我有一个用bash解析xml的脚本。我遇到了其中一个元素的问题,因为它包含一个带有一些命令的字符串: 即:

/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 blabla@bla.com -c bllu@bluu.com -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

ECHO的输出

<![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 mail@mail.com  -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 mail@mail.com -r "MONITORING" -s "** MONITORING - Server URL -Critical **"]]>

1 个答案:

答案 0 :(得分:1)

这有效:

的WriteLine =&#34; $的WriteLine&#34;&#39 ;;&#39;&#34; $线&#34;

...

echo&#34; $ writeLine&#34; &GT;&GT; report.out

谢谢大家的时间!