This question comes from a desire to reduce some repetition in a bash script, around checking if we have something in a file and if so, loading the first line of it into a variable of the same name.
At the moment, there are dozens of lines like this for each variable used throughout:
[[ -s './config/foo' ]] && read -r foo < './config/foo' || echo "problem with ./config/foo file"
[[ -s './config/bar' ]] && read -r foo < './config/bar' || echo "problem with ./config/foo file"
But rather than going through way too many lines like this, I thought I could maybe automate this process by using an array (and also later, expand to other tests with this method).
So I started on the code below, but got stuck at wondering if it's possible to create variable names dynamically? I have no idea how to do that, or if it's even possible. I know I can get the name of the file we would want to use for making the variable name by using ${file##*/}
to strip off the paths (so ./config/foo
becomes foo
for example), but how to turn that result into a variable name and then set it to the contents of the first line of a file, like the original script does?
Here's what I have so far, where DYNAMIC_VARIABLE_NAME_HERE
would be the name we can get from ${file##*/}
:
#!/bin/bash
# check and load config files
required_files=(
'./config/foo'
'./config/bar'
)
for file in "${required_files[@]}"; do
[[ -s "${file}" ]] && read -r DYNAMIC_VARIABLE_NAME_HERE < "${file}" || failed+=("${file}")
done
if [[ ${#failed[@]} -ne 0 ]]; then
echo "there is a problem with the following configuration files:"
for file in "${failed[@]}"; do
echo "${file}"
done
fi
# check
echo "foo: ${foo}"
echo "bar: ${bar}"
Output so far
foo:
bar:
Desired output
foo: [first line of ./config/foo file]
bar: [first line of ./config/bar file]