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
Shell ScriptingLinuxLinux OS Dev

Avatar of undefined
Last Comment
simon3270
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Rohit Bajaj
Flag of India image

ASKER

just want to know one more thing.
can i use source command for a headless linux user
Avatar of Rohit Bajaj
Rohit Bajaj
Flag of India image

ASKER

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

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

Avatar of Rohit Bajaj
Rohit Bajaj
Flag of India image

ASKER

HI,
I am getting an error :
/root/flockApps.properties  syntax error
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

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).
Avatar of Rohit Bajaj
Rohit Bajaj
Flag of India image

ASKER

no not the source version but the awk version.. oh i corrected the script..i didnt give the space between -F= /^
Thanks it works !! :)
Avatar of Rohit Bajaj
Rohit Bajaj
Flag of India image

ASKER

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

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

Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo