简体   繁体   English

Bash脚本:通过源vars.txt定义变量时,没有这样的文件或目录

[英]Bash Script: No such file or directory when defining variable via source vars.txt

I have a script that creates a hash of a file, writes it to vars.txt and then checks that variable next time the script is run to determine if the file has changed. 我有一个脚本,该脚本创建文件的哈希,将其写入vars.txt,然后在下次运行脚本时检查该变量,以确定文件是否已更改。

Here is the script and vars.txt (pastebin) 这是脚本vars.txt (pastebin)

When I run the script I call the variable with source /path/to/file 当我运行脚本时,我使用source /path/to/file调用变量

This now gives me the following error when the file is parsed: 现在,在解析文件时,这给了我以下错误:

/opt/scripts/AutoCommit/vars.txt: line 2: etchttpdconf_dvhost.conf=925ec4d7bd0dc94c3710bcf5fb2c80f422806bb5  /etc/httpd/conf.d/vhost.conf: No such file or directory

On top of that, when the file changes sed is now not replacing the line but adding an additional line underneath. 最重要的是,当文件更改时, sed现在不会替换该行,而是在下面添加了另一行。 It had not done this in the past. 它过去没有这样做。

You get a file that ends up like this. 您得到的文件最终像这样。 Until recently this did not happen so I suspect they are both caused by the same issue. 直到最近这还没有发生,所以我怀疑它们都是由同一问题引起的。

Here is a stripped down version of the script, where I believe the problem may be. 这是该脚本的精简版本,我认为可能是问题所在。

The Problem seems to be that your filename contains multiple dots. 问题似乎是您的文件名包含多个点。 When generating the variable name, only the first one is replaced by an underscore. 生成变量名时,只有第一个用下划线代替。 Having a dot in a variable name results in the cryptic error message you get, but actually is just a syntax error. 在变量名中包含一个点会导致您收到错误的错误消息,但实际上只是语法错误。

To substitute all occurances of a pattern in Bash's "parameter expansion" you have to prepend a / to the pattern. 要替换Bash的“参数扩展”中某个模式的所有出现,您必须在该模式之前加上/ So the fix for your problem is to use the following code in line 6 of your script: 因此,解决问题的方法是在脚本的第6行中使用以下代码:

fileVarName="${fileVarName//./_}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM