简体   繁体   中英

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.

Here is the script and vars.txt (pastebin)

When I run the script I call the variable with 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. 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. So the fix for your problem is to use the following code in line 6 of your script:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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