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
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
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
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].
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 :).
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
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.