Loading ".cfg" file from different directory

Greetings.

In sh or bash How do you load a conf file (full of variables / functions) from a different directory in your script?
That way my main script can be function free so i just have to call the functions from my cfg file.

I have this setup.
Scripts in : $HOME/scripts/shl (it's in my $PATH)
Conf files in : $HOME/scripts/cfg (also equal to $CFG_DIR variable)

I can load the conf file if i put it in the same dir as the script files with

. ./filename.cfg

but it won't work if i use the script from an other directory (through PATH)

I m using different scripts with the same functions / variables and that would be really helpful to get this working..

Any idea?

Thanks

Hi,

It should work, take a look:

$ cat mytest.sh
#!/bin/bash                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                             
MYVAR=value
$ echo $MYVAR

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/bin/site_perl:/usr/bin/core_perl:/home/birei/Public/bins
$ mv mytest.sh ~/Public/bins
$ source mytest.sh
$ echo $MYVAR
value

Regards,
Birei

1 Like

It's more like that :

In function.cfg i have for exemple :

ConfirmOrExit()

{
while true 
   do
   echo -n "Confirmez-vous l'extraction des donn�es depuis $PWD vers $Destination? (o/n) : "
read CONFIRM  
  case $CONFIRM in
         o|O|oui|OUI|Oui|Y|y|YES|yes|Yes)                                                 # Choix oui
         echo
         echo "===================================================================="
         echo " D�but de l'extraction vers $Destination"
         echo "===================================================================="
         echo   
         break 
         
         ;; n|N|non|NON|Non|no|NO|No)                                                      # Choix non
         echo 
         echo "################################"
         echo "# Annulation par l'utilisateur #"
         echo "################################"
         echo 
         exit      
         
         ;; *) echo "Mauvais choix : (o/n)"                                                # Mauvaise r�ponse
      esac 
   done 
}

And in my script.sh I only have

#!/bin/sh

. ./function.cfg
MYVAR=value

ConfirmOrExit       #calling function from function.cfg

command1
command2

This only works if i have both files in the same directory.
I want to put my function .cfg in "../cfg" put the script won't load it. How can i make that happen?

Put your 'function.cfg' in the PATH environment variable as you say in first post.

In your script, put:

source function.cfg
ConfirmOrExit

Does this work? Any error?

Regards,
Birei

1 Like

Ah yes! working perfectly.

Better solution than declaring some new path in your .profile :

#!/bin/sh

export CFG_DIR=/path/do/your/cfg/dir
source $CFG_DIR/function.cfg  

Functions loaded. All good.

Thanks mate!