Pass config file to bash script

I just want to make sure I am understanding how to pass a config file to a bash script . In the below I pass to arguments to a script, then define them in the script as id and config . I then source config using . , if I understand correctly the variables in the config file can now be used by the script? Thank you :).

bash /path/to/script.sh <arg1> <arg2>
arg1=id
arg2=config

contents of config

# COMMENT Line
var1=/path/to/file
# COMMENT Line
var2=/path/to/file
script.sh
#!/bin/bash


# define arg

id=$1  

config=$2 


source config for script to use

. $config

Hi
Try to find out

mkdir TEST && cd $_
echo -e '. $1\necho $var' >script.sh
echo 'var=yes' >config
bash script.sh config
yes
1 Like

I get: y being echo on a newline . Thankk you :).

mkdir TEST && cd $_
echo -e '. $1\necho var' >script.sh
echo 'var=yes' >config
bash script.sh config
yes
y
y
y

should script.sh look like:

$1=config  # define argument

. $config   # source config file in script

echo "$@"  # read contents of config using posistional parameters
echo -e  $1 # echo each line in script
1 Like

I made a mistake in the expression echo -e '. $1\necho var' >script.sh
I fixed it and you probably just copied it before that :slight_smile:
It will be right echo -e '. $1\necho $var' >script.sh

--- Post updated at 17:15 ---

if this line is from script then it is not correct $1=config # define argument
positional parameters are defined as

config=file.txt
next=elsefile.txt
set -- $config $next
echo $1 $2
file.txt elsefile.txt

Or did you mean that when starting a script, the first parameter is defined $1 == config

1 Like

The first parameter starting the script is config , there are two possible choices depending on the data type so I am going to pass it as an argument. Thank you :).

config=$1

set -- $config
echo -e "$@"

but this echos the path to config , I need each line in the config avaliable to the script to use. Do I need to source config ? Thank you.

I think this will work:

config=$1

set -- $config
echo -e "$@"
while read line; do
  echo "$line" >script.sh
done <"$config"

I think now each line in config can be used in [/ICODE]script.sh[/ICODE].

In the script, it is better to immediately indicate the path to the file.

. /path/to/config

You can also add a default value if a file is not specified at startup

. ${1:-/path/to/config}

--- Post updated at 18:48 ---

for example

mkdir TEST && cd $_
echo var=one >config
echo var=two >$HOME/else_config

cat script.sh

. ${1:-$HOME/else_config}
echo $var

launch

bash script config
one
bash script
two
1 Like

if I just print the $line I do see each var printed. However when I re-direct it to the script I do not see them. Are they avaliable to the script to use?

the config file will be different for data type 1 then for data type 2.

For data type 1 the config file will be /path/to/config1 and for data type 2 /path/to/config2[/ICODE, so the /path/to is always the same but the config file may be different. Can

. $config

be used to immediatly source the config file being passed by the argument? Thank you :).

config=$1

echo "$@"
while read line; do
  echo "$line"
done <"$config" >other_file
1 Like

So if I am understanding the below should write each line in config to script.sh to be able to use as variables ? By sourceing . $config the config into memory, that will not print the actual variables but the script will be able to use them, if I understand correctly. Thank you :).

config=$1
. $config
echo "$@"
while read line; do
  echo "$line"
done <"$config" >script.sh

We are confused. As I understand it, all this is superfluous.
You just need to include the config in start of the script and that's it.

. $1

After this expression, your config will virtually be written to the script

--- Post updated at 19:18 ---

Do it and look
cat script.sh

#!/bin/bash
set -x
. $1
... #your commands

And launch

./script.sh config
1 Like

Got it.... if I want to be able to view these var , for say troubleshooting, canI? They can be virtual I just may need to verify them. Thank you :slight_smile:

I think you beat me to it and just posted.

1 Like

I'd be very worried by this approach. You are inviting someone to put something dangerous in a file and mentioning that as a parameter and then you will call/run that unrestricted code to control the main script. Imaging what damage could be done, especially if your main script is called with sudo or is SUID. This could be an easy exploit to damage/destroy/control your server and/or data.

It makes me think of Little Bobby Tables - explain xkcd

Just a thought,
Robin

1 Like