S
S
Sergey Goryachev2016-10-31 03:30:02
linux
Sergey Goryachev, 2016-10-31 03:30:02

How to get bash variables from txt file?

I have a text file which contains 3 lines of text.

db
db_pass
db_pass

And there is a bash script that performs actions with these lines.
sudo mysqldump -u$username -p$password $database > $database.sql

Now I have to manually enter variables, it's not convenient.
Is there any way to get them out of the file?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
ky0, 2016-10-31
@webirus

You write variables to a file, as in a normal sh script - VAR1=foo, VAR2=bar, etc., and at the beginning of your script, say "source /path/to/varsfile", and all variables are pulled from there.

A
abcd0x00, 2016-11-02
@abcd0x00

getlineno()
{
    sed -n "$2{p;q}" "$1"
}

username=`getlineno datafile.txt 2`
password=`getlineno datafile.txt 3`
database=`getlineno datafile.txt 1`

sudo mysqldump -u$username -p$password $database > $database.sql

unset password username database

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question