ianbarker
ianbarker

Reputation: 1253

read variables from file

I'm trying to write a shell script to backup websites, I've got it to go through each directory in vhosts and create a new directory vhosts/{siteurl}/private/backups/ and gzip up the site into there.

However, I also want it to read a file from private that will contain database name, user and password. So I can then use these to perform a mysqldump to private/backups/.

The problem is that I've never written a shell script before so I've got no idea how to read variable data into my script, or how I should format the file to be read.

Hope that's enough information.

Upvotes: 0

Views: 1328

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799430

If the file is in name=value format and you're running bash or sh then you can use . to read and execute the contents of the file.

vars.txt:

foo=123
bar=456

script.sh:

. ./vars.txt
echo "$foo * $bar"

Upvotes: 1

Related Questions