Pass config file to bash script


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Pass config file to bash script
# 1  
Old 01-04-2020
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 Smilie.



Code:
bash /path/to/script.sh <arg1> <arg2>


Code:
arg1=id
arg2=config

contents of config


Code:
# COMMENT Line
var1=/path/to/file
# COMMENT Line
var2=/path/to/file

Code:
script.sh
#!/bin/bash


# define arg

id=$1  

config=$2 


source config for script to use

. $config

# 2  
Old 01-05-2020
Hi
Try to find out
Code:
mkdir TEST && cd $_
echo -e '. $1\necho $var' >script.sh
echo 'var=yes' >config
bash script.sh config
yes


Last edited by nezabudka; 01-05-2020 at 08:20 AM..
This User Gave Thanks to nezabudka For This Post:
# 3  
Old 01-05-2020
I get: y being echo on a newline. Thankk you Smilie.


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

Code:
y
y
y

should script.sh look like:

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


Last edited by cmccabe; 01-05-2020 at 08:51 AM.. Reason: added comments
This User Gave Thanks to cmccabe For This Post:
# 4  
Old 01-05-2020
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 Smilie
It will be right echo -e '. $1\necho $var' >script.sh

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

Quote:
Originally Posted by cmccabe

should script.sh look like:

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

if this line is from script then it is not correct $1=config # define argument
positional parameters are defined as
Code:
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

Last edited by nezabudka; 01-05-2020 at 09:25 AM..
This User Gave Thanks to nezabudka For This Post:
# 5  
Old 01-05-2020
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 Smilie.

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

Last edited by cmccabe; 01-05-2020 at 10:37 AM.. Reason: fixed format, added details
# 6  
Old 01-05-2020
In the script, it is better to immediately indicate the path to the file.
Code:
. /path/to/config

You can also add a default value if a file is not specified at startup
Code:
. ${1:-/path/to/config}

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

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

cat script.sh
Code:
. ${1:-$HOME/else_config}
echo $var

launch
Code:
bash script config
one
bash script
two

This User Gave Thanks to nezabudka For This Post:
# 7  
Old 01-05-2020
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 [ICODE]/path/to/config2[/ICODE, so the /path/to is always the same but the config file may be different. Can

Code:
. $config

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

Last edited by cmccabe; 01-05-2020 at 10:55 AM.. Reason: fixed format
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curl , download file with user:pass in bash script

Hello, My question is about curl command. (ubuntu14.04) In terminal, I am able to download my mainfile with: curl -u user1:pass1 http://11.22.33.44/******* When I convert it into bash script like this: #!/bin/bash cd /root/scripts computer_ip=11.22.33.44 curl -u $1:$2... (8 Replies)
Discussion started by: baris35
8 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

4. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

5. Shell Programming and Scripting

Pass arguments to bash script

myscript.sh #!/bin/bash ARGA=$1 if ; then echo "${ARGA}:Confirmed" else echo "${ARGA}:Unconfirmed" fi when I run the above script from the command line, i run it as: ./myscript.sh jsmith now some times, i need to runn it this way: (8 Replies)
Discussion started by: SkySmart
8 Replies

6. Shell Programming and Scripting

bash script config file

hi all config.sh : dhcp="0" setip="1" telnet="1" ping="1" main.sh function dhcp { } function setip { } (1 Reply)
Discussion started by: sadosan83
1 Replies

7. Shell Programming and Scripting

parsing a config file using bash

Hi , I have a config _file that has 3 columns (Id Name Value ) with many rows . In my bash script i want to be able to parse the file and do a mapping of any Id value so if i have Id of say brand1 then i can use the name (server5X) and Value (CCCC) and so on ... Id Name ... (2 Replies)
Discussion started by: nano2
2 Replies

8. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

9. Web Development

bash script editing my apache config files

okay i'm going to try to say this uber-simple: I use dropbox (file-sync service). in order for dropbox sync files, they must be its children eg. somewhere under /home/jzacsh/Dropbox]. I want to now use it to keep my development files in sync across my machines: easy: just move my dev. files... (2 Replies)
Discussion started by: jzacsh
2 Replies

10. Solaris

bash and its config file

Guys im using bash and for me i think its the best shell, anyway which conf file related to bash ? is it $SHELL or .profile i want to add path to it so i dont need to type the full path of the binary file, i also want to configure the behaviour of the shell, please help. (1 Reply)
Discussion started by: XP_2600
1 Replies
Login or Register to Ask a Question