Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"
share|improve this question

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
share|improve this answer
I wouldn't append ".sh", since it's not meant to be executed. – Ignacio Vazquez-Abrams Feb 2 '12 at 13:15
If open and write something like this backup -site1 i have lot of queries in config file(site2,site3) with different (ssh,pass) is it possible? – macman Feb 2 '12 at 13:18
@macman -site1 would be an argument to your shell script. If you post the shell script in your question, it'd be easier to answer. – slhck Feb 2 '12 at 13:50
here is a picture diagram Imgur – macman Feb 2 '12 at 14:26
-site1 maybe the parameter – macman Feb 2 '12 at 14:44

The other option is just to create a text file and use a while loop to access that file line by line.

share|improve this answer
it maybe works? {scp $user:webfilesbackup-date-date +%Y%m%d.tar.bz2 ~/backup/} in config file> {$user = ''} config file is . how to add this parameter to .command to passing the config file backup -site1 – macman Feb 2 '12 at 17:54
mkdir -p`

...and that will work. This article has more detailed instructions, including how to get custom icons.

You might also look to Automator or AppleScript Studio as ways to build applications that can run sanely, without having to do it by hand.

share|improve this answer
yes but,i searching solution to manually writ "backup -s1" to terminal – macman Feb 2 '12 at 18:43
     # parse the ini like $0.hostname.conf and set the variables
     # place a $0.`hostname -s`.conf file in the same dir as your script
     # set here any VarName=VarValue
     # use ; for comments - you will get host independant conf file 
     # for your bash scripts - [examle usage :][1] 
        eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
            -e 's/;.*$//' \
            -e 's/[[:space:]]*$//' \
            -e 's/^[[:space:]]*//' \
            -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
           < $IniFile \
            | sed -n -e "/^\[$IniSection\]/,/^\s*\[/{/^[^;].*\=.*/p;}"`
     #eof function doParseIniFile
share|improve this answer
Would you mind expanding your code-only answer a little to explain what it does? – slhck Nov 20 '12 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .