使用perl替换复杂密码文件中的一些字符串
Use perl to replace some string in file with complicated password
我想在 bash 脚本中使用 perl 将文本文件中的某些字符串替换为其他可能包含各种特殊字符(密码)的字符串。包含特殊字符的变量来自环境,所以我不知道。
例如
# pw comes from the environment and I cannot be sure about it's content
pw='%$&/|some!\!smart%(]password'
pw=${pw//:/\:}
perl -p -e "s:PASSWORD:$pw:" <<< "my pw is: PASSWORD" # this would come from a text file
# yields: my pw is: %PASSWORD/|some!!smart%(]password
这里我使用:
作为分隔符,将之前可能出现的情况转义,这样应该可以避免一些错误。但是执行此操作确实表明这甚至没有按预期远程工作。 bash 扩展仍在搞乱密码。
现在我的问题是:我怎样才能安全地获取一些环境变量并将其放置在其他地方?什么可能是更好的方法?我当然可以替换和转义未知变量中的更多字符,但我怎么能确定这是否足够?
使用正确的ENV
special variable:
pw='%$&/|some!\!smart%(]password'
export pw=${pw//:/\:}
perl -pe 's:PASSWORD:$ENV{pw}:' <<< "my pw is: PASSWORD"
我想在 bash 脚本中使用 perl 将文本文件中的某些字符串替换为其他可能包含各种特殊字符(密码)的字符串。包含特殊字符的变量来自环境,所以我不知道。
例如
# pw comes from the environment and I cannot be sure about it's content
pw='%$&/|some!\!smart%(]password'
pw=${pw//:/\:}
perl -p -e "s:PASSWORD:$pw:" <<< "my pw is: PASSWORD" # this would come from a text file
# yields: my pw is: %PASSWORD/|some!!smart%(]password
这里我使用:
作为分隔符,将之前可能出现的情况转义,这样应该可以避免一些错误。但是执行此操作确实表明这甚至没有按预期远程工作。 bash 扩展仍在搞乱密码。
现在我的问题是:我怎样才能安全地获取一些环境变量并将其放置在其他地方?什么可能是更好的方法?我当然可以替换和转义未知变量中的更多字符,但我怎么能确定这是否足够?
使用正确的ENV
special variable:
pw='%$&/|some!\!smart%(]password'
export pw=${pw//:/\:}
perl -pe 's:PASSWORD:$ENV{pw}:' <<< "my pw is: PASSWORD"