我有:
COMP_FILE+=docker-compose.con-etl.yml
COMP_FILE+=${PWD}/docker-compose.abc.yml
COMP_FILE+=${PWD}/docker-compose.cde.yml
COMP_FILE+=${PWD}/docker-compose.efg.yml
COMP_FILE+=${PWD}/docker-compose.hij.yml
COMP_FILE+=${PWD}/docker-compose.klm.yml
COMP_FILE+=${PWD}/docker-compose.nmo.yml
COMP_FILE+=${PWD}/docker-compose.pqr.yml
并且我需要在每个docker-compose之前添加-f,例如结果应为
-f docker-compose.con-etl.yml -f ${PWD}/datahub/docker-compose.abc.yml -f ${PWD}/datahub/docker-compose.cde.yml -f ${PWD}/datahub/docker-compose.efg.yml -f ${PWD}/datahub/docker-compose.hij.yml -f ${PWD}/datahub/docker-compose.klm.yml -f ${PWD}/datahub/docker-compose.nmo.yml -f ${PWD}/datahub/docker-compose.pqr.yml
我正在使用以下命令,但无法获得所需的结果并获得
COMPOSE_FILE_ARGS=$(echo ${COMPOSE_FILE[@]/#/-f })
-f docker-compose.con-etl.yml${PWD}/datahub/docker-compose.abc.yml${PWD}/datahub/docker-compose.cde.yml${PWD}/datahub/docker-compose.efg.yml${PWD}/datahub/docker-compose.hij.yml${PWD}/datahub/docker-compose.klm.yml${PWD}/datahub/docker-compose.nmo.yml${PWD}/datahub/docker-compose.pqr.yml
谁能帮我
答案 0 :(得分:1)
您有几个问题。您不是在创建数组,而是在创建长字符串作为第一个元素。要使用命令作为每个元素创建数组,您需要将分配给该数组的值放在(...)
括号中,例如
COMP_FILE+=(docker-compose.con-etl.yml)
COMP_FILE+=(${PWD}/docker-compose.abc.yml)
COMP_FILE+=(${PWD}/docker-compose.cde.yml)
COMP_FILE+=(${PWD}/docker-compose.efg.yml)
COMP_FILE+=(${PWD}/docker-compose.hij.yml)
COMP_FILE+=(${PWD}/docker-compose.klm.yml)
COMP_FILE+=(${PWD}/docker-compose.nmo.yml)
COMP_FILE+=(${PWD}/docker-compose.pqr.yml)
要遍历在数组中添加"-f "
作为每个命令的前缀,可以使用C样式的for循环遍历分配给"-f "
作为前缀的每个索引:
for ((i = 0; i < ${#COMP_FILE[@]}; i++)); do
COMP_FILE[i]="-f ${COMP_FILE[i]}"
done
要确认已添加前缀,只需输出结果数组,
for i in "${COMP_FILE[@]}"; do
echo "$i"
done
输出
$ bash yamlfile.sh
-f docker-compose.con-etl.yml
-f /tmp/tmp-david/docker-compose.abc.yml
-f /tmp/tmp-david/docker-compose.cde.yml
-f /tmp/tmp-david/docker-compose.efg.yml
-f /tmp/tmp-david/docker-compose.hij.yml
-f /tmp/tmp-david/docker-compose.klm.yml
-f /tmp/tmp-david/docker-compose.nmo.yml
-f /tmp/tmp-david/docker-compose.pqr.yml
如果您还有其他问题,请告诉我。
按一行全部编辑每个请求
您已根据需要将信息存储在阵列中。您可以按自己喜欢的任何格式输入内容,例如“全部一行”
echo "${COMP_FILE[@]}"
-f docker-compose.con-etl.yml -f /tmp/tmp-david/docker-compose.abc.yml -f /tmp/tmp-david/docker-compose.cde.yml -f /tmp/tmp-david/docker-compose.efg.yml -f /tmp/tmp-david/docker-compose.hij.yml -f /tmp/tmp-david/docker-compose.klm.yml -f /tmp/tmp-david/docker-compose.nmo.yml -f /tmp/tmp-david/docker-compose.pqr.yml
您只需在需要的任何命令中使用数组中所有元素的扩展,同时确保每个组件都是"-f ......"
。
让我知道这是否对您不利。
答案 1 :(得分:1)
您可以使用cat
和sed
:
$ cat <<'end' | sed 's/=/=" -f /;s/$/"/' > script.sh
COMP_FILE=docker-compose.con-etl.yml
COMP_FILE+=${PWD}/docker-compose.abc.yml
COMP_FILE+=${PWD}/docker-compose.cde.yml
COMP_FILE+=${PWD}/docker-compose.efg.yml
COMP_FILE+=${PWD}/docker-compose.hij.yml
COMP_FILE+=${PWD}/docker-compose.klm.yml
COMP_FILE+=${PWD}/docker-compose.nmo.yml
COMP_FILE+=${PWD}/docker-compose.pqr.yml
end
然后将生成的script.sh源码获取:
$ . script.sh
$ echo $COMP_FILE
-f docker-compose.con-etl.yml -f /home/sergioro/docker-compose.abc.yml -f /home/sergioro/docker-compose.cde.yml -f /home/sergioro/docker-compose.efg.yml -f /home/sergioro/docker-compose.hij.yml -f /home/sergioro/docker-compose.klm.yml -f /home/sergioro/docker-compose.nmo.yml -f /home/sergioro/docker-compose.pqr.yml