【问题标题】:.sh file unable echo out variable that defined in bash.sh 文件无法回显在 bash 中定义的变量
【发布时间】:2019-02-16 06:01:31
【问题描述】:

我正在尝试生成一个带有 .sh 和在 shell 中定义的变量的 HTML 文件

     [root@ip-xxx-xx-x-xxx Reporting]: IMAGE_TAG=xyzabc

.sh文件代码

      #!/bin/bash
      echo "<!DOCTYPE html>" > fargateproductiondeploy.html
      echo "<html> <body>" >> fargateproductiondeploy.html
     echo "<h2>Following Docker container 
     will be deployed  will be Deployed in production up on Approval </h2> 
     <ol>" >> fargateproductiondeploy.html
     echo "<li>$IMAGE_TAG </li>" >> fargateproductiondeploy.html

从 HTML 文件中运行 ./generatehtml.sh 后是

            Following Docker container will be deployed will be Deployed in 
            production upon Approval 
            1.

【问题讨论】:

  • 先做export IMAGE_TAG=xyzabc有效果吗?

标签: bash shell variables scripting


【解决方案1】:

需要先导出变量

$ export IMAGE_TAG=xyzabc
$ ./generatehtml.sh

或源文件

$ IMAGE_TAG=xyzabc
$ . ./generatehtml.sh

最好将值作为参数传递。

#!/bin/bash
image_tag=$1

echo "<!DOCTYPE html>" > fargateproductiondeploy.html
echo "<html> <body>" >> fargateproductiondeploy.html
echo "<h2>Following Docker container will be deployed  will be Deployed in production up on Approval </h2> <ol>" >> fargateproductiondeploy.html
echo "<li>$image_tag </li>" >> fargateproductiondeploy.html

紧随其后

$ ./generatehtml.sh xyzabc

【讨论】:

    猜你喜欢
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 2011-04-26
    相关资源
    最近更新 更多