Loading ".cfg" file from different directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loading ".cfg" file from different directory
# 1  
Old 08-13-2011
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
Code:
. ./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

Last edited by Sekullos; 08-13-2011 at 07:24 PM..
# 2  
Old 08-13-2011
Hi,

It should work, take a look:
Code:
$ 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
This User Gave Thanks to birei For This Post:
# 3  
Old 08-13-2011
It's more like that :

In function.cfg i have for exemple :

Code:
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

Code:
#!/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?
# 4  
Old 08-13-2011
Put your 'function.cfg' in the PATH environment variable as you say in first post.

In your script, put:
Code:
source function.cfg
ConfirmOrExit

Does this work? Any error?

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 5  
Old 08-13-2011
Ah yes! working perfectly.

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

Code:
#!/bin/sh

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

Functions loaded. All good.

Thanks mate!

Last edited by Sekullos; 08-13-2011 at 09:13 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

When loading Linux "loading please wait.." then nothing?

Hi everyone, I have a problem but I have never installed a separate OS before so my lingo and understanding may not be as good as some of you. I will try and explain my problem best I can. I am trying to instead of loading Windows 7 when my computer starts up, for it to start linux specifically... (2 Replies)
Discussion started by: markhow30
2 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. UNIX for Dummies Questions & Answers

look for file size greater than "0" of specific pattern and move those to another directory

Hi , i have some files of specific pattern ...i need to look for files which are having size greater than zero and move those files to another directory.. Ex... abc_0702, abc_0709, abc_782 abc_1234 ...etc need to find out which is having the size >0 and move those to target directory..... (7 Replies)
Discussion started by: dssyadav
7 Replies

6. Shell Programming and Scripting

"find . -printf" without prepended "." path? Getting path to current working directory?

If I enter (simplified): find . -printf "%p\n" then all files in the output are prepended by a "." like ./local/share/test23.log How can achieve that a.) the leading "./" is omitted and/or b.) the full path to the current directory is inserted (enclosed by brackets and a blank)... (1 Reply)
Discussion started by: pstein
1 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

Delete files older than "x" if directory size is greater than "y"

I wrote a script to delete files which are older than "x" days, if the size of the directory is greater than "y" #!/bin/bash du -hs $1 while read SIZE ENTRY do if ; then find $1 -mtime +$2 -exec rm -f {} \; echo "Files older than $2 days deleted" else echo "free Space available"... (4 Replies)
Discussion started by: JamesCarter
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question