Linux read properties file

marcelbirkner / reading-property-from-file.sh

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

#! /bin/sh
PROPERTY_FILE=apps.properties
function getProperty
PROP_KEY= $1
PROP_VALUE= ` cat $PROPERTY_FILE | grep » $PROP_KEY » | cut -d ‘ = ‘ -f2 `
echo $PROP_VALUE
>
echo » # Reading property from $PROPERTY_FILE «
REPOSITORY_URL= $( getProperty » nexus.repository.url » )

good job! thanks..
and for me this line worked —> PROP_VALUE=$(cat $PROPERTY_FILE | grep $PROP_KEY | cut -d’=’ -f2)

This worked for me on all *nix platform:
PROP_VALUE= cat $PROPERTY_FILE | grep «$PROP_KEY» | cut -d’=’ -f2

you can just source the file
source apps.properties
echo $PROP_KEY should give you value

you can just source the file
source apps.properties
echo $PROP_KEY should give you value

Will not work if any property key has dots, e.g. ‘mail.alert.to=yourname@yourdomain.com’

Author’s script won’t work on property values that contain ‘=’, case in point AES hash. Here is what I did,

As @adorogensky mentioned this won’t work if the string itself contains a ‘=’. Simply change ‘-f2’ to ‘-f2-‘
echo «asd=asd=asd» | cut -d’=’ -f2 -> «asd»
echo «asd=asd=asd» | cut -d’=’ -f2- -> «asd=asd»

The list option argument is a comma or whitespace separated set of numbers and/or number ranges. Number ranges consist of a number, a dash (`-‘), and a second number and select the fields or columns from the first number to the second, inclusive. Numbers or number ranges may be preceded by a dash, which selects all fields or columns from 1 to the last number. Numbers or number ranges may be followed by a dash, which selects all fields or columns from the last number to the end of the line. Numbers and number ranges may be repeated, overlapping, and in any order. If a field or column is specified multiple times, it will appear only once in the output. It is not an error to select fields or columns not present in the input line.

Источник

How to Read a Properties File in Linux Shell Scripting

Linux shell scripting is a powerful tool for automating tasks and streamlining workflows in a Linux environment. One common task in Linux shell scripting is reading configuration data from a properties file. A properties file is a simple file format that contains key-value pairs, which can be used to store configuration data for a script or application.

We will walk through a step-by-step example of reading data from a properties file and using it in a shell script. By the end of this tutorial, you will have a clear understanding of how to read properties files in Linux and how to use them in your own scripts and applications.

We will have our properties file config.properties and the properties reader shell script file read_properties_file.sh in this example. Save these two files in the same working directory with the following contents;

Method 1: Using “grep”

config.name=Mamun config.work.hours=39.5 config.location=Ottawa
#!/bin/bash FILE_NAME="config.properties" #read the properties file and returns the value based on the key function getPropVal < value= grep "$" ./$FILE_NAME|cut -d'=' -f2 if [[ -z "$value" ]]; then echo "Key not found" exit 1 fi echo $value > #Get the value with key function testPropertyVal < NAME=($(getPropVal 'config.name')) echo $NAME >#Call the testPropertyVal function testPropertyVal

Method 2: Using the “ source ” or «. ” command

1. Define the properties file: Define the properties file and its location in the shell script. For example, if the properties file is named “config.properties” and is located in the same directory as the shell script.

database.username=CRM_DB database.host=localhost database.password="password!"
PROPS_FILE=./config.properties 

2. Read the properties file: Use the “ source ” or “ . ” command to read the properties file and load the properties into variables in the shell script. For example, if the properties file contains the property database.username with the value admin , you can use the following command to load it into a variable in the shell script:

source $PROPS_FILE USERNAME=$database.username 

This command will load the value of database.username from the properties file into the variable USERNAME in the shell script. 3. Use the properties in the shell script: You can now use the properties in the shell script as you would any other variable. For example, you can use the USERNAME variable in a SQL query to connect to the database:

This command will use the value of the USERNAME variable to connect to the MySQL database. Complete example:

PROPS_FILE=./config.properties source $PROPS_FILE USERNAME=$database.username mysql -u $USERNAME -p

Also, see file manipulation and how to Find file in Linux . See complete examples in our GitHub repository. Follow us on social media

Источник

Reading java .properties file from bash

I am thinking of using sed for reading .properties file, but was wondering if there is a smarter way to do that from bash script?

That is a wonderful question 🙂 In this specific I wanted to see if there is a simple way that I am missing(don’t know about\too stupid to think of etc.)

13 Answers 13

This would probably be the easiest way: grep + cut

# Usage: get_property FILE KEY function get_property

This is what I like the most for simple cases. But I would amend with grep «^$PROP_KEY=» (including the ^ and = delimiters) because property keys are frequently prefix or postfix of other keys.

This will fail on any property that contains an equals sign. A better (more complete, shorter, and even slightly faster) solution would be sed «/^$2=/!d; s///» «$1» which has the same logic as the above grep but then also deletes the matched portion, leaving just the parameter value to be printed.

Properties allow whitespace between the property name and the equals sign. Using the regular expression ^$2 *= with grep or sed allows spaces.

Also, this cuts the value if there are = (equal) chars in the value. Use cut -d’=’ -f2- . See the minus char at the end.

The solutions mentioned above will work for the basics. I don’t think they cover multi-line values though. Here is an awk program that will parse Java properties from stdin and produce shell environment variables to stdout:

BEGIN < FS="="; print "# BEGIN"; n=""; v=""; c=0; # Not a line continuation. >/^\#/ < # The line is a comment. Breaks line continuation. c=0; next; >/\\$/ && (c==0) && (NF>=2) < # Name value pair with a line continuation. e=index($0,"="); n=substr($0,1,e-1); v=substr($0,e+1,length($0) - e - 1); # Trim off the backslash. c=1; # Line continuation mode. next; >/^[^\\]+\\$/ && (c==1) < # Line continuation. Accumulate the value. v= "" v substr($0,1,length($0)-1); next; >((c==1) || (NF>=2)) && !/^[^\\]+\\$/ < # End of line continuation, or a single line name/value pair if (c==0) < # Single line name/value pair e=index($0,"="); n=substr($0,1,e-1); v=substr($0,e+1,length($0) - e); >else < # Line continuation mode - last line of the value. c=0; # Turn off line continuation mode. v= "" v $0; ># Make sure the name is a legal shell variable name gsub(/[^A-Za-z0-9_]/,"_",n); # Remove newlines from the value. gsub(/[\n\r]/,"",v); print n "=\"" v "\""; n = ""; v = ""; > END

As you can see, multi-line values make things more complex. To see the values of the properties in shell, just source in the output:

cat myproperties.properties | awk -f readproperties.awk > temp.sh source temp.sh 

The variables will have ‘_’ in the place of ‘.’, so the property some.property will be some_property in shell.

If you have ANT properties files that have property interpolation (e.g. ‘$’) then I recommend using Groovy with AntBuilder.

I have corrected a bug with the script when a multi-line value is the last defined and ends with a blank-line

I wrote a script to solve the problem and put it on my github.

One option is to write a simple Java program to do it for you — then run the Java program in your script. That might seem silly if you’re just reading properties from a single properties file. However, it becomes very useful when you’re trying to get a configuration value from something like a Commons Configuration CompositeConfiguration backed by properties files. For a time, we went the route of implementing what we needed in our shell scripts to get the same behavior we were getting from CompositeConfiguration . Then we wisened up and realized we should just let CompositeConfiguration do the work for us! I don’t expect this to be a popular answer, but hopefully you find it useful.

If you want to use sed to parse -any- .properties file, you may end up with a quite complex solution, since the format allows line breaks, unquoted strings, unicode, etc: http://en.wikipedia.org/wiki/.properties

One possible workaround would using java itself to preprocess the .properties file into something bash-friendly, then source it. E.g.:

line_a : "ABC" line_b = Line\ With\ Breaks! line_c = I'm unquoted :( 
line_a="ABC" line_b=`echo -e "Line\nWith\nBreaks!"` line_c="I'm unquoted :(" 

Of course, that would yield worse performance, but the implementation would be simpler/clearer.

Not tested, and should be more tolerant of leading/trailing spaces, comments etc., but you get the idea. Whether you use Perl (or another language) over sed is really dependent upon what you want to do with the properties once you’ve parsed them out of the file.

Note that (as highlighted in the comments) Java properties files can have multiple forms of delimiters (although I’ve not seen anything used in practice other than colons). Hence the split uses a choice of characters to split upon.

Ultimately, you may be better off using the Config::Properties module in Perl, which is built to solve this specific problem.

That won’t necessarily work. The keys and values of the properties file can be delimited by spaces, the = character, or by the : character (there might be other ways too). I believe you can also mix and match which delimiters you use in a single properties file.

I have some shell scripts that need to look up some .properties and use them as arguments to programs I didn’t write. The heart of the script is a line like this:

dbUrlFile=$(grep database.url.file etc/zocalo.conf | sed -e "s/.*: //" -e "s/#.*//") 

Effectively, that’s grep for the key and filter out the stuff before the colon and after any hash.

if you want to use «shell», the best tool to parse files and have proper programming control is (g)awk. Use sed only simple substitution.

I have sometimes just sourced the properties file into the bash script. This will lead to environment variables being set in the script with the names and contents from the file. Maybe that is enough for you, too. If you have to do some «real» parsing, this is not the way to go, of course.

Hmm, I just run into the same problem today. This is poor man’s solution, admittedly more straightforward than clever;)

decl=`ruby -ne 'puts chomp.sub(/=(.*)/,%q<="\1";>).gsub(".","_")' my.properties` eval $decl 

then, a property ‘my.java.prop’ can be accessed as $my_java_prop.

This can be done with sed or whatever, but I finally went with ruby for its ‘irb’ which was handy for experimenting. It’s quite limited (dots should be replaced only before ‘=’,no comment handling), but could be a starting point.

@Daniel, I tried to source it, but Bash didn’t like dots in variable names.

I have had some success with

 PROPERTIES_FILE=project.properties function source_property < local name=$1 eval "$name=\"$(sed -n '/^'"$name"'=/,/^[A-Z]\+_*[A-Z]*=/p' $PROPERTIES_FILE|sed -e 's/^'"$name"'=//g' -e 's/"/\\"/g'|head -n -1)\"" >source_property 'SOME_PROPERTY' 

This is a solution that properly parses quotes and terminates at a space when not given quotes. It is safe: no eval is used.

I use this code in my .bashrc and .zshrc for importing variables from shell scripts:

# Usage: _getvar VARIABLE_NAME [sourcefile. ] # Echos the value that would be assigned to VARIABLE_NAME _getvar() < local VAR="$1" shift awk -v Q="'" -v QQ='"' -v VAR="$VAR" ' function loc(text) < return index($0, text) >function unquote(d) < $0 = substr($0, eq+2) d; print substr($0, 1, loc(d)-1) > < sub(/^[ \t]+/, ""); eq = loc("=") >substr($0, 1, eq-1) != VAR < next ># assignment is not for VAR: skip loc("=" QQ) == eq < unquote(QQ); exit >loc("=" Q) == eq < unquote( Q); exit > < print substr($1, eq + 1); exit >' "$@" > 

This saves the desired variable name and then shifts the argument array so the rest can be passed as files to awk .

Because it’s so hard to call shell variables and refer to quote characters inside awk , I’m defining them as awk variables on the command line. Q is a single quote (apostrophe) character, QQ is a double quote, and VAR is that first argument we saved earlier.

For further convenience, there are two helper functions. The first returns the location of the given text in the current line, and the second prints the content between the first two quotes in the line using quote character d (for «delimiter»). There’s a stray d concatenated to the first substr as a safety against multi-line strings (see «Caveats» below).

While I wrote the code for POSIX shell syntax parsing, that appears to only differ from your format by whether there is white space around the asignment. You can add that functionality to the above code by adding sub(/[ \t]*=[ \t]*/, » (line 6) or =’ (line 7) or no quotes (line 8). Each of those lines prints the assigned value.

Caveats: If there is an escaped quote character, we’ll return a value truncated to it. Detecting this is a bit nontrivial and I decided not to implement it. There’s also a problem of multi-line quotes, which get truncated at the first line break (this is the purpose of the «stray d » mentioned above). Most solutions on this page suffer from these issues.

Источник

Читайте также:  Установить maven на linux
Оцените статью
Adblock
detector