reading values of some parameters from a file in a linux shell script

HI,
I am creating a shell script in which i will be running a command :
mvn jetty:run -DFLOCK_APPS_CONFIG=staging -Dport=8080
from inside the directory : /root/flock-snippets
Now the parameters staging and 8080 which i am passing above needs to be actually read from a file residing at /root named flockApps.properties
The contents of the file are :
FLOCK_APP_CONFIG=staging
PORT=8080

Open in new window


So basically my script should read the values of those two keys and run the mvn command by passing the appropriate values.

Please help/guide me to build the script.

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simon3270Commented:
If that's all that is in the flockApps.properties file, you can "source" the file, and that will define those two values as variables.  So:
source /root/flockApps.properties

mvn jetty:run -DFLOCK_APPS_CONFIG=${FLOCK_APP_CONFIG} -Dport=${PORT}

Open in new window


If there are othjer lines in the file, you 'll have to extract the required data, but the above would be the easiest way.

By the way, you will often see a single . as a synonym for "source", so ". /root/flockApps.properties" (note the space between the dot and the filename).
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simon3270Commented:
If there are other lines, you could do something like:
PORT=$(awk -F= '/^PORT=/{print $2}')
FLOCK_APP_CONFIG=$(awk -F= '/^FLOCK_APP_CONFIG=/{print $2}')

Open in new window

to define the two variables.
1
Rohit BajajAuthor Commented:
just want to know one more thing.
can i use source command for a headless linux user
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Rohit BajajAuthor Commented:
HI,
Also in the code that you provide :
PORT=$(awk -F= '/^PORT=/{print $2}')
FLOCK_APP_CONFIG=$(awk -F= '/^FLOCK_APP_CONFIG=/{print $2}')

How do i pass filename to it . Because i will be making a script to do all this.
And so once i have the PORT and FLOCK... i will run mvn command.
0
simon3270Commented:
Not sure what you mean by a "headless Linux user".  The source command can happily go into a script - no user required!

Ah, I knew I'd forget something.  The filename goes at the end of the awk command, so the script would have:
PORT=$(awk -F= '/^PORT=/{print $2}' /root/flockApps.properties)
FLOCK_APP_CONFIG=$(awk -F= '/^FLOCK_APP_CONFIG=/{print $2}' /root/flockApps.properties)

mvn jetty:run -DFLOCK_APPS_CONFIG=${FLOCK_APP_CONFIG} -Dport=${PORT}

Open in new window

0
Rohit BajajAuthor Commented:
HI,
I am getting an error :
/root/flockApps.properties  syntax error
0
simon3270Commented:
Is that in the "source" version?  I think that means that you have other bits in that file.  As I said, if it *only* contains the two lines you showed, you can source it.  I f it contains other text, you shoulduse the awk version (where you do *not* source it).
0
Rohit BajajAuthor Commented:
no not the source version but the awk version.. oh i corrected the script..i didnt give the space between -F= /^
Thanks it works !! :)
0
Rohit BajajAuthor Commented:
One more thing if you can help me with is.
This script exactly can i put inside init.d as an initScript or will it require change..
as i want this to get executed everytime the system restarts
0
simon3270Commented:
It should work fine in an init script, with a couple of changes.

The PATH and other environment variables will probably be different from what you have whne you log in, so it is best to put the full path to all programs referenced, so /usr/bin/awk, /usr/local/bin/mvn and so on (those are examples - I don't knwo where those programs are on your system).  There may also be other variables that you need to set (I remember setting mvn variables when I used to use it).  Just add those variables to the script.

You should also specify the shell you want to use, so have something like
  #!/bin/bash
as the first line of the script.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.