替换/替换shell脚本中的对抗性子字符串变量



我有三个未映射的对抗性shell变量。

$mystring
$old
$new

记住,这三个字符串都是对抗性的。它们将包含特殊字符。他们将包含一切可能的混乱的更换。如果你的替换中有漏洞,字符串就会利用它。

在$mystring中用$new替换$old最简单的函数是什么?(我找不到任何适用于所有情况的通用替换的堆栈溢出解决方案(。

这里没有什么特别之处——要确保您的值在参数扩展中被视为文字,您唯一需要做的就是确保您引用的是搜索值,如BashFAQ#21:的相关部分所述

result=${mystring/"$old"/$new}

如果内部没有双引号,$old将被解释为fnmatch风格的glob表达式;对他们来说,这是字面意义上的。


要对流进行操作,请考虑gsub_literal,也在BashFAQ#21:中进行了描述

# usage: gsub_literal STR REP
# replaces all instances of STR with REP. reads from stdin and writes to stdout.
gsub_literal() {
# STR cannot be empty
[[ $1 ]] || return
# string manip needed to escape ''s, so awk doesn't expand 'n' and such
awk -v str="${1//\/\\}" -v rep="${2//\/\\}" '
# get the length of the search string
BEGIN {
len = length(str);
}
{
# empty the output string
out = "";
# continue looping while the search string is in the line
while (i = index($0, str)) {
# append everything up to the search string, and the replacement string
out = out substr($0, 1, i-1) rep;
# remove everything up to and including the first instance of the
# search string from the line
$0 = substr($0, i + len);
}
# append whatever is left
out = out $0;
print out;
}
'
}
some_command | gsub_literal "$search" "$rep"

还可以使用以下技术(再次取自之前链接的常见问题解答(对文件进行原位替换:

# Using GNU tools to preseve ownership/group/permissions
gsub_literal "$search" "$rep" < "$file" > tmp &&
chown --reference="$file" tmp &&
chmod --reference="$file" tmp &&
mv -- tmp "$file"

最新更新