构建Bash变量名称

时间:2016-08-24 07:09:00

标签: bash macos sh

我想为彩色输出创建一个函数(只是为了更好地学习bash)

这是有效的

    <!--Email_Cover_Page-->

    

                <!-- Speakers -->

           <table class="speakers" style=" border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;background: #fa4b00;" align="center" border="0" cellpadding="0" cellspacing="0" width="600">
              <tbody>

                <tr>
                  <td style=" border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;font-size:0px; line-height:0px; mso-line-height-rule: exactly;" align="center" height="30" valign="top">&nbsp;</td>
                    </tr>

                    <tr>
                        <td align="center" valign="top">
                          <table class="container" style=" border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;" align="center" border="0" cellpadding="0" cellspacing="0" width="600">
                            <tbody>
                                <tr>
                                    <td>
                                        <table class="container" style=" border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;" align="center" border="0" cellpadding="0" cellspacing="0" width="600" >
                                          <tbody>
                                            <tr>
                                                <td class="h1" style=" border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt; background-image:url(../title-bg2.png); background-size:cover; background-position:30px;font-family: 'Montserrat', sans-serif;font-size: 26px; color:white;text-align:left; letter-spacing: normal; padding-left:50px;padding-bottom: 30px;padding-right:10px;line-height: 22px;" align="center">
                                                    <img src="../speaker_icon.png" width="25" height="25" style="vertical-align: middle; border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;"><span style="font-weight:600;"><span class="wrap_textbox">Speakers</span></span>
                                                </td>
                                             </tr>
                                           </tbody>
                                        </table>
                                     </td>
                                </tr>





                                <!--Speakers Images & Designation-->
                            <tr>
                                <table>
                                    <tr>
                                        <td>
                                            <!--Speaker 1 -->
                                            <table  style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt; margin: 0 auto;  " align="left" border="0" cellpadding="0" cellspacing="0" >
                                                <tbody>
                                                <tr>
                                                    <td align="center">
                                                        <table style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;" align="center" border="0" cellpadding="0" cellspacing="0" width="120">
                                                            <tbody><tr>
                                                                <td align="center" height="100" valign="top" width="100">
                                                                    <p style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;"><a class="modimg" style="text-decoration: none!important;" href="#"><img style="outline: none;text-decoration: none;-ms-interpolation-mode: bicubic;border: none; " alt="" src="../mahesh_murthy.jpeg" border="0" height="110" width="110" ></a></p>

                                                                </td>
                                                            </tr>
                                                            </tbody></table>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="h2" style=" font-weight: bold ;font-family: 'proxima_nova_rgregular', Arial, helvetica, sans-serif;font-size: 10px;padding-top: 5px;padding-bottom: 0px;text-align: center;color:#ffffff;">
                                                        <span class="wrap_textbox">Mr. Mahesh Murthy</span>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="h2" style="font-family: 'proxima_nova_rgregular', Arial, helvetica, sans-serif;font-size: 8px;text-align: center;color: #ffffff;">
                                                        <span class="wrap_textbox">Co-Founder at SeeFund</span>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>


                                            <!--Speaker 2-->

                                            <table  style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt; margin: 0 auto; margin-left: 15px;  " align="left" border="0" cellpadding="0" cellspacing="0" >
                                                <tbody>
                                                <tr>
                                                    <td align="center">
                                                        <table style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;" align="center" border="0" cellpadding="0" cellspacing="0" width="120">
                                                            <tbody><tr>
                                                                <td align="center" height="100" valign="top" width="100">
                                                                    <p style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;"><a class="modimg" style="text-decoration: none!important;" href="#"><img style=" border: 3px solid antiquewhite ; outline: none;text-decoration: none;-ms-interpolation-mode: bicubic;" alt="" src="../R_chandrashekhar.jpeg" border="0" height="110" width="110" ></a></p>
                                                                </td>
                                                            </tr>
                                                            </tbody></table>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="h2" style=" font-weight: bold ;font-family: 'proxima_nova_rgregular', Arial, helvetica, sans-serif;font-size: 10px;padding-top: 5px;padding-bottom: 0px;text-align: center;color:#ffffff; ">
                                                        <span class="wrap_textbox">Mr. R Chandrasekhar</span>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="h2" style="font-family: 'proxima_nova_rgregular', Arial, helvetica, sans-serif;font-size: 8px;text-align: center;color: #ffffff;">
                                                        <span class="wrap_textbox">President of NASSCOM</span>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>



                                            <!--Speaker 3 -->

                                            <table  style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt; margin: 0 auto;  margin-left: 15px;  " align="left" border="0" cellpadding="0" cellspacing="0" >
                                                <tbody>
                                                <tr>
                                                    <td align="center">
                                                        <table style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;" align="center" border="0" cellpadding="0" cellspacing="0" width="120">
                                                            <tbody><tr>
                                                                <td align="center" height="100" valign="top" width="100">
                                                                    <p style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;"><a class="modimg" style="text-decoration: none!important;" href="#"><img style=" border: 3px solid rgba(255, 255, 255, 0.7) ; outline: none;text-decoration: none;-ms-interpolation-mode: bicubic;" alt="" src="../Jay_panda.jpeg" border="0" height="110" width="110" ></a></p>
                                                                </td>
                                                            </tr>
                                                            </tbody></table>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="h2" style=" font-weight: bold ;font-family: 'proxima_nova_rgregular', Arial, helvetica, sans-serif;font-size: 10px;padding-top: 5px;padding-bottom: 0px;text-align: center;color:#ffffff;">
                                                       <span class="wrap_textbox">Mr. Jay Panda</span>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="h2" style="font-family: 'proxima_nova_rgregular', Arial, helvetica, sans-serif;font-size: 8px;text-align: center;color: #ffffff;">
                                                        <span class="wrap_textbox">Member of Parliament</span>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>



                                            <!--Speaker 4 -->


                                            <table  style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt; margin: 0 auto;  margin-left: 15px;  " align="left" border="0" cellpadding="0" cellspacing="0" >
                                                <tbody>
                                                <tr>
                                                    <td align="center">
                                                        <table style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;" align="center" border="0" cellpadding="0" cellspacing="0" width="120">
                                                            <tbody><tr>
                                                                <td align="center" height="100" valign="top" width="100">
                                                                    <p style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;"><a class="modimg" style="text-decoration: none!important;" href="#"><img style=" border: 3px solid rgba(255, 255, 255, 0.7) ; outline: none;text-decoration: none;-ms-interpolation-mode: bicubic;" alt="" src="../Shazia_Ilmi.jpeg" border="0" height="110" width="110" ></a></p>
                                                                </td>
                                                            </tr>
                                                            </tbody></table>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="h2" style=" font-weight: bold ;font-family: 'proxima_nova_rgregular', Arial, helvetica, sans-serif;font-size: 10px;padding-top: 5px;padding-bottom: 0px;text-align: center;color:#ffffff;">
                                                        <span class="wrap_textbox">Ms. Shazia Ilmi</span>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="h2" style="font-family: 'proxima_nova_rgregular', Arial, helvetica, sans-serif;font-size: 8px;text-align: center;color: #ffffff;">
                                                        <span class="wrap_textbox">Spokesperson at BJP</span>
                                                    </td>
                                                </tr>
                                                </tbody>


                                    </tr>

                                    <tr>
                                        <td style="font-size:0px; line-height:0px; mso-line-height-rule: exactly;" align="center" height="5" valign="top">&nbsp;</td>
                                    </tr>
         <table style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;" align="center" border="0" cellpadding="0" cellspacing="0" width="120">
                                                                            <tbody><tr>
                                                                                <td align="center" height="100" valign="top" width="100">
                                                                                    <p style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;"><img style=" border: 3px solid rgba(255, 255, 255, 0.7) ; outline: none;text-decoration: none;-ms-interpolation-mode: bicubic; padding-top: 15px;" alt="" src="../indextb.jpg" border="0" height="40" width="90" ></p>
                                                                                </td>
                                                                            </tr>
                                                                            </tbody></table>
                                                                    </td>
                                                                </tr>

                                                                </tbody>
                                                            </table>


                                                            <table  style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt; margin: 0 auto; margin-left: 15px;  " align="left" border="0" cellpadding="0" cellspacing="0" >
                                                                <tbody>
                                                                <tr>
                                                                    <td align="center">
                                                                        <table style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;" align="center" border="0" cellpadding="0" cellspacing="0" width="120">
                                                                            <tbody><tr>
                                                                                <td align="center" height="100" valign="top" width="100">
                                                                                    <p style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;" ><img style=" border: 3px solid rgba(255, 255, 255, 0.7) ; outline: none;text-decoration: none;-ms-interpolation-mode: bicubic; padding-top: 15px;" alt="" src="../transstadia.jpg" border="0" height="40" width="90" ></p>
                                                                                </td>
                                                                            </tr>
                                                                            </tbody></table>
                                                                    </td>
                                                                </tr>

                                                                </tbody>
                                                            </table>


                                                            <table  style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt; margin: 0 auto;  margin-left: 15px;  " align="left" border="0" cellpadding="0" cellspacing="0" >
                                                                <tbody>
                                                                <tr>
                                                                    <td align="center">
                                                                        <table style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;" align="center" border="0" cellpadding="0" cellspacing="0" width="120">
                                                                            <tbody><tr>
                                                                                <td align="center" height="100" valign="top" width="100">
                                                                                    <p style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;"><img style=" border: 3px solid rgba(255, 255, 255, 0.7) ; outline: none;text-decoration: none;-ms-interpolation-mode: bicubic; padding-top: 15px;" alt="" src="../claris.jpg" border="0" height="40" width="90" ></p>
                                                                                </td>
                                                                            </tr>
                                                                            </tbody></table>
                                                                    </td>
                                                                </tr>

                                                                </tbody>
                                                            </table>



                                                            <table  style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt; margin: 0 auto;  margin-left: 15px;  " align="left" border="0" cellpadding="0" cellspacing="0" >
                                                                <tbody>
                                                                <tr>
                                                                    <td align="center">
                                                                        <table style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;" align="center" border="0" cellpadding="0" cellspacing="0" width="120">
                                                                            <tbody><tr>
                                                                                <td align="center" height="100" valign="top" width="100">
                                                                                    <p style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;"><img style=" border: 3px solid rgba(255, 255, 255, 0.7) ; outline: none;text-decoration: none;-ms-interpolation-mode: bicubic; vertical-align: middle;" alt="" src="../arvind.jpg" border="0" height="70" width="70"  ></p>
                                                                                </td>
                                                                            </tr>
                                                                            </tbody></table>
                                                                    </td>
                                                                </tr>
                                                                </tbody>
                                                            </table>


                                                        </td>

                                                    </tr>
</table>
                                                  

                                                    

不,我尝试构建功能

ESC_SEQ="\x1b["
# foreground red
FG_RED="${ESC_SEQ}31;"
# background yellow
BG_YELLOW="43;"
# style bold
FS_BOLD="1m"
# echo
echo -e "${FG_RED}${BG_YELLOW}${FS_BOLD}Hello World!"

如何根据参数构建回声执行?

2 个答案:

答案 0 :(得分:1)

export ESC_SEQ="\e["
export YELLOW="43m"

function format_text() 
{
  BG="$ESC_SEQ$1"
  echo -e "${BG}Hello World!"
}

format_text $YELLOW

我不知道如何在没有临时变量的情况下实现。所有examples我发现使用tmp变量来实现它。

答案 1 :(得分:1)

以下脚本应该是一个很好的起点:

#!/bin/bash


ext_echo()
{
  declare -A colors

  colors=(
    [red]="<red>"
    [blue]="<blue>"
  )

  for c in "$@"; do
    echo ${colors[$c]}
  done
}

ext_echo red blue

<强>输出

<red>
<blue>