我试图创建yaml文件从一个模板,使用我的变量。我的yaml模板看起来像这样
number: {{NUMBER}}
name: {{NAME}}
region: {{REGION}}
storenum: {{STORENUM}}
clients: {{CLIENTS}}
tags: {{TAGS}}
storename: {{STORENAME}}
employee: {{EMPLOYEE}}
products: {{PRODUCTS}}
但我的变量是在一个CSV文件的结构是变量。
Number - Name - Region - Storenum
StoreX - StoreX - New York - 30
我现在有一个小脚本,从一个模板创建变量参数和模板像这样script. shtemplate. yml-fvariables.txt。结果是这样的
number: 37579922
name: Store1
region: New York
storenum: 32
clients: 100
tags: stores
storename: Store newyork
employee: 10
products: 200
但我只能一个接一个地做。有没有办法读取CSV参数并发送到程序并生成例如Template 1,Template 2,..从CSV参数?任何帮助
#!/bin/bash
readonly PROGNAME=$(basename $0)
config_file="<none>"
print_only="false"
silent="false"
usage="${PROGNAME} [-h] [-d] [-f] [-s] --
where:
-h, --help
Show this help text
-p, --print
Don't do anything, just print the result of the variable expansion(s)
-f, --file
Specify a file to read variables from
-s, --silent
Don't print warning messages (for example if no variables are found)
examples:
VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt
${PROGNAME} test.txt -f my-variables.txt
${PROGNAME} test.txt -f my-variables.txt > new-test.txt"
if [ $# -eq 0 ]; then
echo "$usage"
exit 1
fi
if [[ ! -f "${1}" ]]; then
echo "You need to specify a template file" >&2
echo "$usage"
exit 1
fi
template="${1}"
if [ "$#" -ne 0 ]; then
while [ "$#" -gt 0 ]
do
case "$1" in
-h|--help)
echo "$usage"
exit 0
;;
-p|--print)
print_only="true"
;;
-f|--file)
config_file="$2"
;;
-s|--silent)
silent="true"
;;
--)
break
;;
-*)
echo "Invalid option '$1'. Use --help to see the valid options" >&2
exit 1
;;
# an option argument, continue
*) ;;
esac
shift
done
fi
vars=$(grep -oE '\{\{[A-Za-z0-9_]+\}\}' "${template}" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//')
if [[ -z "$vars" ]]; then
if [ "$silent" == "false" ]; then
echo "Warning: No variable was found in ${template}, syntax is {{VAR}}" >&2
fi
fi
# Load variables from file if needed
if [ "${config_file}" != "<none>" ]; then
if [[ ! -f "${config_file}" ]]; then
echo "The file ${config_file} does not exists" >&2
echo "$usage"
exit 1
fi
source "${config_file}"
fi
var_value() {
eval echo \$$1
}
replaces=""
# Reads default values defined as {{VAR=value}} and delete those lines
# There are evaluated, so you can do {{PATH=$HOME}} or {{PATH=`pwd`}}
# You can even reference variables defined in the template before
defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}' "${template}" | sed -e 's/^{{//' -e 's/}}$//')
for default in $defaults; do
var=$(echo "$default" | grep -oE "^[A-Za-z0-9_]+")
current=`var_value $var`
# Replace only if var is not set
if [[ -z "$current" ]]; then
eval $default
fi
# remove define line
replaces="-e '/^{{$var=/d' $replaces"
vars="$vars
$current"
done
vars=$(echo $vars | sort | uniq)
if [[ "$print_only" == "true" ]]; then
for var in $vars; do
value=`var_value $var`
echo "$var = $value"
done
exit 0
fi
# Replace all {{VAR}} by $VAR value
for var in $vars; do
value=$(var_value $var | sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g") # '&' and <space> is escaped
if [[ -z "$value" ]]; then
if [ $silent == "false" ]; then
echo "Warning: $var is not defined and no default is set, replacing by empty" >&2
fi
fi
# Escape slashes
value=$(echo "$value" | sed 's/\//\\\//g');
replaces="-e 's/{{$var}}/${value}/g' $replaces"
done
escaped_template_path=$(echo $template | sed 's/ /\\ /g')
eval sed $replaces "$escaped_template_path"
2条答案
按热度按时间polkgigr1#
但我只能一个接一个地做。有没有办法读取CSV参数并发送到程序并生成例如Template 1,Template 2,..从CSV参数?任何帮助
我不能拿出一个完整的解决方案,但我有一个解决方案,可以创建多个文件。把它融入你的剧本。
file.csv
Template.yaml
通过运行以下命令检查创建的文件:
输出为:
envsubst
和sed
,因为export
是bash
shell的内置。none
。cuxqih212#
这从来没有得到一个公认的答案。我提供第二个部分解决方案。
从这个开始:
最后的outfile只是一个虚拟文件,它将管道结果存储在一个文件中。你可以对管道输出做任何你想做的事情。
这里有两件事是错误的:它引入了一些你不想要的引号。它生成JSON而不是YAML。
Powershell Gallery中有一个名为Powershell-Yaml的模块。它包含一个类似于ConvertTo-Json的工具。你可以导入这个模块,然后把它修复成你想要的样子。