Svan
Svan

Reputation: 11

bash script to read info from a file

how can i make a bash script to tell the script this:

i will tell the bash like:

#!/bin/bash
include /usr/local/serverinfo.txt or.sh

rsync -avz $location root@$host:/$location2

all of this $location, $host , $location2, to be entered in /usr/local/serverinfo.txt

how can i tell the bash script to get this infos from the file,

if i put them in the same file will work just perfect, however i whant it to be outside of the file, any ideea?

let me know, thanks.

Upvotes: 0

Views: 93

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74695

You can use source, or equivalently ., which takes another file as an argument and executes it. This assumes that the file you are sourceing contains valid bash syntax.

script.sh:

#!/bin/bash
var=1

source inc.sh          # or . inc.sh
echo $var

inc.sh

var=2

output:

2

Upvotes: 5

timgeb
timgeb

Reputation: 78790

This depends on how the data in serverinfo.txt is formatted. If it is a simple list like

/this/is/the/first/location
/this/is/the/host
/this/is/the/second/location

then you can do

location=$(sed -n '1p' /path/to/serverinfo.txt)
host=$(sed -n '2p' /path/to/serverinfo.txt)
location2=$(sed -n '3p' /path/to/serverinfo.txt)

Upvotes: 0

Related Questions