Linux 如何替换占位符的值

Linux 如何替换占位符的值,linux,bash,shell,Linux,Bash,Shell,我不熟悉shell编程。我有两个文件: eg.txt file.sh txt文件包含一些HTML内容,file.sh包含一个shell脚本。 在脚本中,为temp变量指定了一个值,该值应注入HTML文件中 eg.txt <html> Hi MGM ,<br/> One alert has been received !!<br/> Here is the event Data.<br/><br/> <font si

我不熟悉shell编程。我有两个文件:

  • eg.txt
  • file.sh
  • txt文件包含一些HTML内容,file.sh包含一个shell脚本。 在脚本中,为
    temp
    变量指定了一个值,该值应注入HTML文件中

    eg.txt

    <html>
      Hi MGM ,<br/>
      One alert has been received !!<br/>
    
     Here is the event Data.<br/><br/>
    
     <font size=‘1’>{temp}</font>
     <br/><br/>
     Regards,
     WDTS Supports.
     </html>
    
    To:%s
    Subject: Alert Updates !!
    Content-Type: text/html
    
    <html>
      Hi MGM ,<br/>
      One alert has been received !!<br/>
    
     Here is the event Data.<br/><br/>
    
     <font size=‘1’>%s</font>
     <br/><br/>
     Regards,
     WDTS Supports.
     </html>
    
    对于sed:

    sed "s/{temp}/$temp/" eg.txt | /usr/sbin/sendmail -t
    
    您还可以使用
    printf
    在模板中插入变量:

    file.sh

    echo $1
    temp=56
    
    (
    echo "To:"$1
    echo "Subject: Alert Updates !! "
    echo "Content-Type: text/html"
    
    echo cat eg.txt
    ) | /usr/sbin/sendmail -t
    
    echo "Mail sent !!"
    
    temp=56
    tpl=$(cat "eg.txt")
    
    printf "$tpl" "$1" "$temp" | /usr/sbin/sendmail -t
    
    eg.txt

    <html>
      Hi MGM ,<br/>
      One alert has been received !!<br/>
    
     Here is the event Data.<br/><br/>
    
     <font size=‘1’>{temp}</font>
     <br/><br/>
     Regards,
     WDTS Supports.
     </html>
    
    To:%s
    Subject: Alert Updates !!
    Content-Type: text/html
    
    <html>
      Hi MGM ,<br/>
      One alert has been received !!<br/>
    
     Here is the event Data.<br/><br/>
    
     <font size=‘1’>%s</font>
     <br/><br/>
     Regards,
     WDTS Supports.
     </html>
    
    对于sed:

    sed "s/{temp}/$temp/" eg.txt | /usr/sbin/sendmail -t
    
    您还可以使用
    printf
    在模板中插入变量:

    file.sh

    echo $1
    temp=56
    
    (
    echo "To:"$1
    echo "Subject: Alert Updates !! "
    echo "Content-Type: text/html"
    
    echo cat eg.txt
    ) | /usr/sbin/sendmail -t
    
    echo "Mail sent !!"
    
    temp=56
    tpl=$(cat "eg.txt")
    
    printf "$tpl" "$1" "$temp" | /usr/sbin/sendmail -t
    
    eg.txt

    <html>
      Hi MGM ,<br/>
      One alert has been received !!<br/>
    
     Here is the event Data.<br/><br/>
    
     <font size=‘1’>{temp}</font>
     <br/><br/>
     Regards,
     WDTS Supports.
     </html>
    
    To:%s
    Subject: Alert Updates !!
    Content-Type: text/html
    
    <html>
      Hi MGM ,<br/>
      One alert has been received !!<br/>
    
     Here is the event Data.<br/><br/>
    
     <font size=‘1’>%s</font>
     <br/><br/>
     Regards,
     WDTS Supports.
     </html>
    

    我在您的代码中引入了一些错误检查:

    #!/bin/bash
    temp=56
    if [ -z "$1" ]
    then
    echo "Usage : ./file.sh user_name_to_mail to"
    exit -1
    else
        if id "$1" >/dev/null 2>&1 #Check if user exists, suppress stdout, stderr
        then
          mail_header=$(echo -e "To: $1\nSubject: Alert Updates"'!!'"\nContent-Type: text/html\n")
          mail_body=$(awk -v var="$temp" '{print gensub(/{temp}/,var,"g"$0)}' eg.txt)
          echo -e "$mail_header\n$mail_body" | sendmail -t                   
        else
          echo -e "Sorry! Invalid User\n"
          exit -1 # The error code is set to  detect failure
        fi
    fi
    
    为了防止邮件成为垃圾邮件,您需要为发送电子邮件的域提供有效的SPF记录。检查起点


    注意:

    是bash的一个特殊字符,用于引用前面的命令。为了解决这个问题,我使用了
    .Updates“!!”\n内容类型..


    单引号内的
    失去了它的特殊意义


    有趣的阅读:

  • 什么是唱片
  • 打开SPF

  • 我在您的代码中引入了一些错误检查:

    #!/bin/bash
    temp=56
    if [ -z "$1" ]
    then
    echo "Usage : ./file.sh user_name_to_mail to"
    exit -1
    else
        if id "$1" >/dev/null 2>&1 #Check if user exists, suppress stdout, stderr
        then
          mail_header=$(echo -e "To: $1\nSubject: Alert Updates"'!!'"\nContent-Type: text/html\n")
          mail_body=$(awk -v var="$temp" '{print gensub(/{temp}/,var,"g"$0)}' eg.txt)
          echo -e "$mail_header\n$mail_body" | sendmail -t                   
        else
          echo -e "Sorry! Invalid User\n"
          exit -1 # The error code is set to  detect failure
        fi
    fi
    
    为了防止邮件成为垃圾邮件,您需要为发送电子邮件的域提供有效的SPF记录。检查起点


    注意:

    是bash的一个特殊字符,用于引用前面的命令。为了解决这个问题,我使用了
    .Updates“!!”\n内容类型..


    单引号内的
    失去了它的特殊意义


    有趣的阅读:

  • 什么是唱片
  • 打开SPF


  • 我想除了sed的
    g
    没有什么害处,但为什么要麻烦呢?;)我正在使用此命令,但电子邮件将成为垃圾邮件。谁能告诉我怎么处理这个问题吗。它应该出现在我的收件箱中,尝试删除
    来自例如中的主题。txt@Kenavoz我正在使用多个温度,如温度1、温度2等,但它并没有给我答案。如何使用温度变量来实现功率?你应该用sed编辑你的问题除了
    g
    之外没有坏处,我想,但为什么要麻烦呢?;)我正在使用此命令,但电子邮件将成为垃圾邮件。谁能告诉我怎么处理这个问题吗。它应该出现在我的收件箱中,尝试删除
    来自例如中的主题。txt@Kenavoz我正在使用多个温度,如温度1、温度2等,但它并没有给我答案。如何使用温度变量来实现功率?你应该编辑你的问题注意:-无用地使用
    cat
    。使用这个命令,但是电子邮件会变成垃圾邮件。如何解决这个问题。电子邮件应该放在收件箱中。注意:-无用地使用
    cat
    。使用此命令,但电子邮件会变成垃圾邮件。如何解决这个问题。电子邮件应该放在收件箱里。记住
    在bash中有特殊的含义。检查。记住
    在bash中有特殊的含义。检查。您可以简化:
    如果id“$1”>/dev/null 2>&1;那么
    我不同意,老实说,我不认为它比:
    如果这个东西;然后
    。在命令替换中换行意味着OP熟悉
    $?
    。即使是这样,还是要练习教书。@andlrc:嗯。。。已更改:-)。如果id为“$1”>/dev/null 2>,则可以简化:
    &1;那么
    我不同意,老实说,我不认为它比:
    如果这个东西;然后
    。在命令替换中换行意味着OP熟悉
    $?
    。即使是这样,还是要练习教书。@andlrc:嗯。。。已更改:-)。