control a bash script with variables from an external file
With shell scripts, this is normally accomplished using the source
function, which executes the file as a shell script as if it was inlined into the script you're running -- which means that any variables you set in the file are exported into your script.
The downside is that (a) your config file is executed, so it's a security risk if unprivileged users can edit privileged config files. And (b) your config file syntax is restricted to valid bash syntax. Still, it's REALLY convenient.
config.conf
USER=joe
PASS=hello
SERVER=127.0.0.2
script.sh
#!/bin/bash
# Set defaults
USER=`whoami`
# Load config values
source config.conf
foobar2000 --user=$USER --pass=$PASS --HOST=$HOST
source
can be abbreviated with a single .
-- so the following two are equivalent:
source file.sh
. file.sh
Something like this. The important bit is using read to grab a line as an array.
#!/bin/bash
configfile=/pathtocontrolfile
cat $configfile | while read -a HR ; do
[[ -z ${HR[0]} ]] && continue # skip empty lines
USER1=${HR[0]}
HOST1=${HR[1]}
PW1=${HR[2]}
USER2=${HR[3]}
HOST2=${HR[4]}
PW2=${HR[5]}
imapsync \
--buffersize 8192000 --nosyncacls --subscribe --syncinternaldates --IgnoreSizeErrors \
--host1 $HOST1 --user1 $USER1 --password1 $PW1 --ssl1 --port1 993 --noauthmd5 \
--host2 $HOST2 --user2 $USER2 --password2 $PW2 --ssl2 --port2 993 --noauthmd5 --allowsizemismatch
done