Bash脚本:通过源定义变量时没有这样的文件或目录vars.txt
Bash Script: No such file or directory when defining variable via source vars.txt
我有一个创建文件散列的脚本,将其写入 vars.txt,然后在下次脚本运行时检查该变量 运行 确定文件是否已更改。
Here is the script and vars.txt(粘贴)
当我 运行 脚本时,我用 source /path/to/file
调用变量
现在在解析文件时出现以下错误:
/opt/scripts/AutoCommit/vars.txt: line 2: etchttpdconf_dvhost.conf=925ec4d7bd0dc94c3710bcf5fb2c80f422806bb5 /etc/httpd/conf.d/vhost.conf: No such file or directory
最重要的是,当文件更改时 sed
现在不是替换该行,而是在下面添加一个额外的行。它过去没有这样做过。
您得到的文件最终是 like this. 直到最近才发生这种情况,所以我怀疑它们都是由同一个问题引起的。
Here is a stripped down version of the script, where I believe the problem may be.
问题似乎是您的文件名包含多个 点。生成变量名时,只有第一个用下划线代替。变量名中有一个点会导致您收到神秘的错误消息,但实际上只是一个语法错误。
要替换 Bash 的 "parameter expansion" 中所有出现的模式,您必须在模式前添加 /
。因此,解决您的问题的方法是在脚本的第 6 行中使用以下代码:
fileVarName="${fileVarName//./_}"
我有一个创建文件散列的脚本,将其写入 vars.txt,然后在下次脚本运行时检查该变量 运行 确定文件是否已更改。
Here is the script and vars.txt(粘贴)
当我 运行 脚本时,我用 source /path/to/file
现在在解析文件时出现以下错误:
/opt/scripts/AutoCommit/vars.txt: line 2: etchttpdconf_dvhost.conf=925ec4d7bd0dc94c3710bcf5fb2c80f422806bb5 /etc/httpd/conf.d/vhost.conf: No such file or directory
最重要的是,当文件更改时 sed
现在不是替换该行,而是在下面添加一个额外的行。它过去没有这样做过。
您得到的文件最终是 like this. 直到最近才发生这种情况,所以我怀疑它们都是由同一个问题引起的。
Here is a stripped down version of the script, where I believe the problem may be.
问题似乎是您的文件名包含多个 点。生成变量名时,只有第一个用下划线代替。变量名中有一个点会导致您收到神秘的错误消息,但实际上只是一个语法错误。
要替换 Bash 的 "parameter expansion" 中所有出现的模式,您必须在模式前添加 /
。因此,解决您的问题的方法是在脚本的第 6 行中使用以下代码:
fileVarName="${fileVarName//./_}"