MacOS – Can I create a configuration file for a shell script


Is it possible to create a .sh shell or .command file with a configuration file?

I need to backup websites via Terminal and be able to put some data to configuration files,
and have the shell execute it.

For example, the username, password, ssh id, et cetera.

ssh "tar cjvf webfilesbackup-date-`date +%Y%m%d`.tar.bz2 public_html/"    
ssh "mysqldump -u user_admin -ppass database_1 > databasebackup-`date +%Y%m%d`-db.sql"
scp`date +%Y%m%d`.tar.bz2 ~/backup/
scp`date +%Y%m%d`-db.sql ~/backup/
ssh "rm -f webfilesbackup-date-`date +%Y%m%d`.tar.bz2"
ssh "rm -f databasebackup-`date +%Y%m%d`-db.sql"

Best Answer

Create a file (e.g. settings) with your settings as variables:


Then source the file from your script and you will be able to access the variables:

. settings
echo $username     # <- this will print "foo"

Concerning your second "question", if you have multiple hosts you can just have multiple config files and source the one passed as an argument to your script. For example, create a config file called site1.

Call your script with ./ site1

. $1               # <- this will load the file "site1"
echo $username     # <- this will print "foo"

With your backup script, this would mean:

. $1
ssh "tar cjvf webfilesbackup-date-`date +%Y%m%d`.tar.bz2 public_html/"
ssh "mysqldump -u $user -p$pass $database > databasebackup-`date +%Y%m%d`-db.sql"
# ... and so on