I'm working on a backup script that manages multiple external HDDs, but I'm trying to streamline how I specify the paths associated with each drive. Instead of manually setting all the paths, I want to simply specify the drive names, which should link to their correct paths. For example, I might have a configuration that looks like: `driveA=(/pathA /pathB /pathD)`.
Currently, I'm using variable names as drive identifiers and namerefs (`declare -n`), but that has a couple of drawbacks. Notably, Bash variable names can't include dashes (`-`), which would be useful for drive naming, and I'd prefer to separate the variable definitions into a configuration file instead of keeping everything in the script.
I'm looking for recommendations on a standard way to parse a configuration in a way that will be reliable and easy to manage all these paths associated with drives. The format doesn't have to stick to what I've mentioned, but I prefer to have one config file to keep everything organized.
5 Answers
Using a delimited format can simplify things! Each drive can have a unique ID, so you might consider a setup like this:
```
UUID:source path:destination path
```
You can get the UUIDs from your drives using `blkid`. Your backup script can read from this config file, allowing you to easily loop through the drives and their paths. Here's a quick example:
```bash
while read -r uuid; do
while IFS=":" read -r _ source dest; do
# Your backup steps here
done < <(grep "^${uuid}" /path/to/backup.conf)
done < <(sudo blkid -s UUID -o value)
```
This keeps it straightforward and uses simple string parsing!
Using a format like TOML works great for this kind of configuration too! It can easily define your drives and paths in a clean manner that’s easy to read and maintain. You might find it less cumbersome compared to traditional bash-only solutions.
Keep it simple! Just remember to separate your drive identifiers from the paths with a delimiter to avoid conflicts. Something like "|" works well since it's seldom found in path names!
Honestly, if the configurations get too complex, you might want to switch to Python or another programming language that handles nested structures better than Bash. If you stick with Bash, just make sure your drive names don’t conflict with variable names and consider organizing your data effectively with delimiters. You can load paths into arrays which simplifies the way you manage multiple values.
For something more robust, consider using a service registry like Consul or Etcd. They can store variables in a structured way and make retrieval seamless. With a service like this, you can fetch configurations using a simple command instead of worrying about formatting your own files. Here's a quick sample for fetching keys:
```bash
for key in $(curl -s http://localhost:8500/v1/kv/myapp/config/?keys); do
var_name=$(basename "$key")
var_value=$(curl -s http://localhost:8500/v1/kv/"$key"?raw)
export "$var_name"="$var_value"
done
```
This will load all your relevant configuration into environment variables automatically!

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically