Shell 读取文件并替换${1}、${2}。。。带字符串的值

Shell 读取文件并替换${1}、${2}。。。带字符串的值,shell,Shell,我有一个文件template.txt,其内容如下: param1=${1} param2=${2} param3=${3} 我想用scriptParams变量的元素替换${1}、{2}、${3}……${n}字符串值 下面的代码仅替换第一行 scrpitParams="test1,test2,test3" cat template.txt | for param in ${scriptParams} ; do i=$((++i)) ; sed -e "s/\${$i}/$param/" ; d

我有一个文件template.txt,其内容如下:

param1=${1}
param2=${2}
param3=${3}
我想用scriptParams变量的元素替换${1}、{2}、${3}……${n}字符串值

下面的代码仅替换第一行

scrpitParams="test1,test2,test3"

cat template.txt | for param in ${scriptParams} ; do i=$((++i)) ; sed -e "s/\${$i}/$param/" ; done
结果:

param1=test1
param2=${2}
param3=${3}
param1=test1
param2=test2
param3=test3
期望值:

param1=test1
param2=${2}
param3=${3}
param1=test1
param2=test2
param3=test3
注意:我不想保存替换的文件,我想使用它的替换值。

学习调试:

cat template.txt | for param in ${scriptParams} ; do i=$((++i)) ; echo $i - $param; done

1 - test1,test2,test3
哎呀

scriptParams="test1 test2 test3"

cat template.txt | for param in ${scriptParams} ; do i=$((++i)) ; echo $i - $param; done

1 - test1
2 - test2
3 - test3
好的,看起来好多了

cat template.txt | for param in ${scriptParams} ; do i=$((++i)) ; sed -e "s/\${$i}/$param/" ; done
param1=test1
param2=${2}
param3=${3}
哎呀。。。那有什么问题?第一个
sed
命令“吃掉”所有输入。您还没有构建一个管道,其中一个
sed
命令为下一个命令提供信息。。。您有三个
sed
s试图读取相同的输入。显然,第一个处理了整个输入

OK,让我们采用一种不同的方法,让我们为单个<代码> SED命令创建一个参数(注释:<代码> > <代码>,以强制<代码>回音< /代码>不解释<代码> -E >代码>作为命令行开关。 就这样。请注意,这并不完美,如果替换文本很复杂(例如:包含空格),您可能会遇到各种问题

让我想想如何更好地做到这一点。。。(好吧,我想到的一个明显的解决方案是不使用shell脚本来完成此任务…)

更新:
如果您想构建一个合适的管道,这里有一些解决方案:

您可以仅使用bash来实现这一点:

#!/bin/bash
scriptParams=("test1" "test2" "test3")  ## Better store it as arrays.
while read -r line; do
    for i in in "${!scriptParams[@]}"; do  ## Indices of array scriptParams would be populated to i starting at 0.
        line=${line/"\${$((i + 1))}"/"${scriptParams[i]}"}  ## ${var/p/r} replaces patterns (p) with r in the contents of var. Here we also add 1 to the index to fit with the targets.
    done
    echo "<br>$line</br>"
done < template.txt

如果要使用数组,请使用实数组<代码>sed也不需要:

$ cat template
param1=${1}
param2=${2}
param3=${3}

$ scriptParams=("test one" "test two" "test three")
$ while read -r l; do for((i=1;i<=${#scriptParams[@]};i++)); do l=${l//\$\{$i\}/${scriptParams[i-1]}}; done; echo "$l"; done < template
param1=test one
param2=test two
param3=test three
$cat模板
param1=${1}
param2=${2}
param3=${3}
$scriptParams=(“测试一”“测试二”“测试三”)

$whileread-rl;代办((i=1;你的代码片段中的i
scrpitParams
不是数组,另外还有一个拼写错误,因为你在第二行代码中的意思可能是
scriptParams
。@AdrianFrühwirth:他没有尝试将其用作数组。@KarolyHorvath我知道,但可能OP没有-这只是一个错误的术语。我无法使用真正的数组,我正在尝试重新使用它。)放置SH文件自定义参数,这些参数不存储在数组中。顺便说一下,您的代码适用于数组。我创建了另一个数组变量并使用了此方法。请将上面的文件名更正为模板。txt@korkuteren我不确定我是否理解您的第一条评论,但我很高兴它对您有效。您的代码给出了错误-->sed:-e expression#1,char 2:未知命令:`-'在这里工作,不知道您在做什么。您是否有其他模板文件或
scriptParams
?您使用了两行代码。当我按以下方式处理您的脚本时,无法获得预期的结果。您的脚本:
code
scriptParams=“test1,test2,test3”sedargs=$(对于${scriptParams}中的参数);doi=$(++i));echo“-e”s/\${$i}/$param/”done)cat template.txt | sed$sedargs
code
结果:param1=test1,test2,test3 param2=${2}param3=${3}不,我的脚本:
scriptParams=“test1 test2 test3”
。您可以用
sed
替换