Jan Pastyřík Jan Pastyřík - 3 months ago 15
Bash Question

Reading variables from config file in Shell

I have question about Linux shell scripts. My question is realy abstract, so may not make sense. The idea is having 1 script and 2 config files.

Script can be like (drinkOutput.sh):

#!/bin/bash

echo -e " $1 \n"
echo -e " $2 \n"


First Config file contain (beer.conf):

drink1="heineken"
drink2="argus"


Second Config file contain (vine.conf):

drink1="chardonnay"
drink2="hibernal"


The key thing is calling the script. It has to be in next format (or with parameter)

./drinkOutput.sh beer.conf


In this case I need to have in $1 heineken and in $2 argus (inside of drinkOutput script). For

./drinkOutput.sh vine.conf


I need to get back into drinkOutput.sh chardonnay and hibernal.

Does anybody know? Thanks for any tips

Answer

You can source the config files if they are in the right format (and it seems it is in your example).

drinkOutput()
{
    echo "$1"
    echo "$2"
}

conf="$1"
source "$conf"
drinkOutput "$drink1" "$drink2"