linux - Bash 脚本 : parsing string with a command without executing it

标签 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 **"]]>

最佳答案

这有效:

writeLine="$writeLine"';'"$line"

...

echo "$writeLine">> report.out

感谢大家的宝贵时间!

关于linux - Bash 脚本 : parsing string with a command without executing it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15295120/

相关文章:

python - Ubuntu Python : "get_mac" function not defined

python - 如何在使用 Python 3 和 asyncio 实时写入文件时读取文件,例如 "tail -f"

python - 使用 7zip 和 python 在给定路径中创建受密码保护的文件

linux - 解析日志以查找每个用户的所有源 IP 的最快方法

linux - 将 bash 参数中的多个不同内容添加到名为的 sql 脚本中的变量集

node.js - 使用 NVM 设置 Node 版本或安装(如果不可用)。

python - 如何在启动时在 Python IDLE 中预加载 Python 模块?

linux - 基于公共(public)列合并两个文件

linux - bash:循环中的 grep 不 grep

android - ADB Start Activity 名称不起作用