Load config file at commandline


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Load config file at commandline
# 1  
Old 12-01-2014
Load config file at commandline

Hello,

This would be very basic question to most of you, but for me as a newbie it is an un-know.

I would like to load a configuration file into memory before executing a specific command. For example: I have a config file that holds all the database connectivity information and I'd like to load this specific db config file in order to execute a command:
config file will have:
Code:
export user=xyz
export password=abc
export database=123

Code:
mysql -u $user -password $password -db $database

I need the values correctly read from the config file that I will load before executing the above command.
Help is much appreciated.
# 2  
Old 12-01-2014
I'd rather not export password's....

Make a script that looks like:
Code:
#!/bin/bash
source /path/to/password-file
mysql -u $user -password $password -db $database

Password-file
Code:
user=xyz
password=abc
database=123

This way, the variable 'password' is also emptied when the script ends.

Hope this helps
This User Gave Thanks to sea For This Post:
# 3  
Old 12-01-2014
Hello Sea,

Thanks for the quick turn around, that's one way of doing it which I am aware of. But I need something that I can keep loading dynamically just before executing the command. Password was just an example I provided, I have other variables that I need to load in other scenarios. I need something that I need to do at command line.
like:
Code:
. ./.myconfigFile
mysql -u $user -password $password -db $database

do we have something like that in linux, to load configuration at command line dynamically, apart from the one way I have posted in this reply.
# 4  
Old 12-01-2014
This is exactly how it's done. Why do you want to change it?
# 5  
Old 12-01-2014
Your request and the example code of both of us is all the same.
I'm not understanding your question.
# 6  
Old 12-01-2014
Quote:
Originally Posted by babyPen1985
Code:
export user=xyz
export password=abc
export database=123

Apart from what sea and RudiC already said (and which i can wholeheartedly agree with) here are some other things to consider:

Code:
export variable=value

is a quite common malpractice but still a malpractice. "export" is a command (in fact a "reserved word") and a variable declaration is a command. You cannot and should not mix that up. Do it this way:

Code:
variable=value
export variable

or, if you insist on one line:

Code:
variable=value ; export variable

btw., the effect of "export" on a variable is lasting. You do not need the also oftenly seen:

Code:
variable=value ; export variable
variable=newvalue ; export variable
variable=othervalue ; export variable
....

Once a variable is exported, it stays exported ("exported" means: its current value is copied to the environment of any child process it creates) and if you change its value then this new value instead of the old one would be copied to a newly inherited child.

If you "dot-execute" a script like this:

Code:
. /some/script.sh


this means that it is executed in the current envrronment instead of creating a new one at the start which is closed at the end (which is the default). This is why changes to the environment are lasting. On the other hand you can use any shell device there is, including control structures. You could accept a parameter and set your environment accordingly, like in the following sketch:

Code:
#! /bin/ksh

if [ $# -ne 0 ] ; then
     print -u2 - "Error: provide a parameter!"
     exit 2
fi

case "$1" in
     "one")
          user=userone
          database=dbone
          password=pwone
          ;;

     "two")
          user=usertwo
          database=dbtwo
          password=pwtwo
          ;;

     "three")
          user=userthree
          database=dbthree
          password=pwthree
          ;;

     *)
          print -u2 - "Error: unkown value, use only \"one\", \"two\" or \"three\","
          exit 1
          ;;

esac

exit 0

which you would call in this way:

Code:
. /path/to/script one
. /path/to/script two
. /path/to/script three

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add a line in every file with perl commandline?

Hi, i want to add a line at the beginning of every file in a directory. perl -i.bkp -p -e 'print "#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset\n" if $. == 1' *.reset But this command is updating only the first file in the directory. I think this is because $. is not resetting... (3 Replies)
Discussion started by: twistedpair
3 Replies

2. UNIX for Dummies Questions & Answers

VPS has load 200, httpd load no activity, netstat nothing

Hello, on my hostserver i see one VPS of mine got load of 200.00 and netstat nothing (not a single blank line on netstat command) after some time, netstat started showing connections, but i see no excessive IP connections. tail -f /var/log/httpd/access_log shows no activity /var/log/messages ;... (1 Reply)
Discussion started by: postcd
1 Replies

3. Red Hat

Apache virtual host config vs global config problem

Hi folks, I am trying to configure Apache webserver and also a virtual host inside this webserver. For Global server config: /var/www/html/index.html For virtual host config: /var/www/virtual/index.html Both client10 & www10 are pointing to 192.168.122.10 IP address. BUT, MY... (1 Reply)
Discussion started by: freebird8z
1 Replies

4. Shell Programming and Scripting

How to print and append output of nawk script in commandline and as well into a file?

Hi All, I am working on nawk script, has the small function which prints the output on the screen.Am trying to print/append the same output in a file. Basically nawk script should print the output on the console/screen and as well it should write/append the same result to a file. script :... (3 Replies)
Discussion started by: Optimus81
3 Replies

5. 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

6. Shell Programming and Scripting

parsing config file to create new config files

Hi, I want to use a config file as the base file and parse over the values of country and city parameters in the config file and generate separate config files as explained below. I will be using the config file as mentioned below: (config.txt) country:a,b city:1,2 type:b1... (1 Reply)
Discussion started by: clazzic
1 Replies

7. Shell Programming and Scripting

Need help in wrting Load Script for a Load-Resume type of load.

hi all need your help. I am wrting a script that will load data into the table. then on another load will append the data into the existing table. Regards Ankit (1 Reply)
Discussion started by: ankitgupta
1 Replies

8. UNIX for Dummies Questions & Answers

trying to strip the first 4 char. of a file out via commandline

im kind alost. i beleave its a sed command. but i cant seem to find it in my book. can someone point me in the write direction. i know this is extreamly sloppy. but this is what i did untill i can figure out how to manipulate the filename namespace. an ls on the directory where this would run... (2 Replies)
Discussion started by: Optimus_P
2 Replies
Login or Register to Ask a Question