Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of Rohit Bajaj

ASKER

just want to know one more thing.
can i use source command for a headless linux user
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.
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

HI,
I am getting an error :
/root/flockApps.properties  syntax error
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).
no not the source version but the awk version.. oh i corrected the script..i didnt give the space between -F= /^
Thanks it works !! :)
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
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.