bash - .sh 文件无法回显在 bash 中定义的变量
问题描述
我正在尝试使用在 shell 中定义的 .sh 和变量生成 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
$ ./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
推荐阅读
- pointers - 指针的指针的内存地址
- java - Android BLE write Characteristic 锁定 onCharacteristicWrite/onCharacteristicChange
- java - 当我运行我的开关盒并选择一个选项时,它将不断循环该选项
- javascript - TextureLoader 在 Three.js 中不起作用。我如何让它工作?
- android - React-native:组件不会在android上的屏幕上渲染超过某个点
- entity-framework - 如何使用 LINQ for EF6 进行查询,具有多对多关系的数据库优先方法
- python - Pandas 中分类变量的顺序
- python-3.x - 使用特定令牌的 Python 请求重定向
- r - 在 R 中构造 For 循环和 if 循环
- python - 仅在数组数据 Python3 上打印数字