无法替换文件内容。

huangapple go评论65阅读模式
英文:

Unable to replace the file contents

问题

我正在尝试使用 perl 命令在另一个 shell 脚本中替换文件的内容:

文件名:INFA.param

内容:

$ParamUser=SOMEUSERNAME
$ParamPswd=SOMEPASSWORD
$$StartTime=2021-06-26 08:31:51:1195079
$$EndTime=2021-06-26 09:31:51:1195079

当我在另一个名为 "pp.sh" 的 shell 脚本中打开上述文件时

cat INFA.param > temp.param
. ./temp.param
pmcmdpassword=$(pmcmd $password -e CRYPTDATA)
perl -pe -e "s#ParamPswd=\Q$ParamPswd\E#ParamPswd=$pmcmdpassword#g" INFA.param

我得到一些错误:

=SOMEUSERNAME: not found
=SOMEPASSWORD: not found
31850942StartTime=2021-06-26 08:31:51:1195079: not found
31850942EndTime=2021-06-26 09:31:51:1195079: not found

你能否建议我应该如何替换密码。

英文:

I am trying to replace the contents of file using perl command in another shell script:

Filename: INFA.param

Contents:

$ParamUser=SOMEUSERNAME
$ParamPswd=SOMEPASSWORD
$$StartTime=2021-06-26 08:31:51:1195079
$$EndTime=2021-06-26 09:31:51:1195079

When I open the above file in another shell script "pp.sh"

cat INFA.param > temp.param
. ./temp.param
pmcmdpassword=$(pmcmd $password -e CRYPTDATA)
perl -pe -e "s#ParamPswd=\Q$ParamPswd\E#ParamPswd=$pmcmdpassword#g" INFA.param

I am getting some errors:

=SOMEUSERNAME: not found
=SOMEPASSWORD: not found
31850942StartTime=2021-06-26 08:31:51:1195079: not found
31850942EndTime=2021-06-26 09:31:51:1195079: not found 

Could you please suggest me how I should replace the password.

答案1

得分: 1

你只能在文件被编写为有效的shell源代码时才能安全使用.source。这不适用于你这里的文件:shell赋值不以$开头。

如果你尝试通过重新加密来修改现有密码,这就是你的代码试图将原始文件读入shell解释器的原因,那么你可能想要:

#!/usr/bin/env bash
#              ^^^^- 不是sh,仅限bash

declare -A variables=( )
while IFS='=' read -r key value; do
  variables[$key]=$value
done < INFA.param

oldPassword=${variables['$ParamPswd']}
newPassword=$(pmcmd "$oldPassword" -e CRYPTDATA)

in="$oldPassword" out="$newPassword" \
  perl -pi -e 's/\Q$ENV{"in"}/$ENV{"out"}/g' INFA.param

如果你的新密码是输入的话,那就更简单了;那么你根本不需要在第一次阅读原始文件时:

newPassword=$(pmcmd "${password?Password undefined}" -e CRYPTDATA)

out="$newPassword" \
  perl -pi -e 's/^$ParamPswd=.*$/$ENV{"out"}/g' INFA.param
英文:

You can only safely use . or source when a file was written to be valid shell source code. That is not true of the file you have here: shell assignments don't start with $.

If you're trying to modify the existing password by reencrypting it, and that's why your code tries to read the original file into the shell interpreter, then you might want:

#!/usr/bin/env bash
#              ^^^^- NOT sh, ONLY bash

declare -A variables=( )
while IFS=&#39;=&#39; read -r key value; do
  variables[$key]=$value
done &lt; INFA.param

oldPassword=${variables[&#39;$ParamPswd&#39;]}
newPassword=$(pmcmd &quot;$oldPassword&quot; -e CRYPTDATA)

in=&quot;$oldPassword&quot; out=&quot;$newPassword&quot; \
  perl -pi -e &#39;s/\Q$ENV{&quot;in&quot;}/$ENV{&quot;out&quot;}/g&#39; INFA.param

It's even easier if your new password is input; then you don't need to read the original file at all in the first place:

newPassword=$(pmcmd &quot;${password?Password undefined}&quot; -e CRYPTDATA)

out=&quot;$newPassword&quot; \
  perl -pi -e &#39;s/^[$]ParamPswd=.*$/$ENV{&quot;out&quot;}/g&#39; INFA.param

答案2

得分: 0

作为`bash`脚本执行以下内容:
```shell
$ParamUser=SOMEUSERNAME
$ParamPswd=SOMEPASSWORD
$$StartTime=2021-06-26 08:31:51:1195079
$$EndTime=2021-06-26 09:31:51:1195079

与Perl不同,bash仅使用$来扩展变量。例如:

a='some text'
echo "$a"

在bash中输出:

some text

当您的脚本启动时,bash变量ParamUser为空,因此$ParamUser=SOMEUSERNAME这一行将被扩展为=SOMEUSERNAME。这不是一个有效的命令,因此bash会报错:

=SOMEUSERNAME: not found

对于bash

$$StartTime=2021-06-26 08:31:51:1195079

存在多个不同的问题,其中包括间距。另外,$$会扩展为您当前进程的PID,显然是31850942。

因此,一个解决方案是:

ParamUser=$(sed -n 's/ParamUser=//p' INFA.param)

(其他变量类似)

请注意,行

pmcmdpassword=$(pmcmd $password -e CRYPTDATA)

使用了在问题的脚本部分中未设置的变量password


<details>
<summary>英文:</summary>

You are executing the following as `bash` script:

$ParamUser=SOMEUSERNAME
$ParamPswd=SOMEPASSWORD
$$StartTime=2021-06-26 08:31:51:1195079
$$EndTime=2021-06-26 09:31:51:1195079


Unlike Perl, bash uses the `$` only to expand variables. For example:

a='some text'
echo "$a"

gives in bash:

some text

When your script starts, the bash variable `ParamUser` is empty, so the line `$ParamUser=SOMEUSERNAME` will be expanded to `=SOMEUSERNAME`. That is not a valid command, and therefore bash complains

=SOMEUSERNAME: not found

For bash

$$StartTime=2021-06-26 08:31:51:1195079

contains multiple different problems, among which the spacing. Also, `$$` expands to the PID of your current process, which is 31850942, apparently.

So, a solution would be

ParamUser=$(sed -n 's/ParamUser=//p' INFA.param)

(same for the rest)

Note that the line

pmcmdpassword=$(pmcmd $password -e CRYPTDATA)

uses a variable `password` that is not set in the script-part in the question.

</details>



huangapple
  • 本文由 发表于 2023年6月26日 16:55:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定