Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a .properties file which contains keys that have a period character using Shell script

I am trying to read a properties file from a shell script which contains a period (.) character like below:

# app.properties db.uat.user=saple user db.uat.passwd=secret   #/bin/sh function pause(){    read -p "$*" }  file="./app.properties"  if [ -f "$file" ] then     echo "$file found."  . $file  echo "User Id " $db.uat.user echo "user password =" $db.uat.passwd else     echo "$file not found." fi 

I have tried to parse the file after sourcing the file but it is not working since the keys contains the "." character and there are spaces in that value also.

My properties file always resides in the same directory of the script or somewhere in /usr/share/doc

like image 650
Kiran Avatar asked Mar 03 '15 11:03

Kiran


People also ask

How do you read a properties file in Shell?

ReadProperty.sh In the above script, the method getProperty() method identifies the Property file to be read and fetches the entire row using the Key (i.e., Name). Now it will have the complete string i.e., Name=Admin. Our motto is to fetch only the Value i.e., Admin.

How read properties file in Linux?

In this article, we learned how to parse a properties file. First, we kept the properties as bash variables at the cost of modifying their names. Next, we stored them in the associative array, which returned the value bound to a given name. Finally, we just searched for properties in the file using grep.

How do I view properties file in Unix?

Typing ls -l at the command prompt, users will see a whole host of properties associated with each file displayed, as shown in Figure 2.1. The main properties displayed are: The file type and access permissions.


2 Answers

I use simple grep inside function in bash script to receive properties from .properties file.

This properties file I use in two places - to setup dev environment and as application parameters.

I believe that grep may work slow in big loops but it solves my needs when I want to prepare dev environment.

Hope, someone will find this useful.

Example:

File: setup.sh

#!/bin/bash  ENV=${1:-dev}  function prop {     grep "${1}" env/${ENV}.properties|cut -d'=' -f2 }  docker create \     --name=myapp-storage \     -p $(prop 'app.storage.address'):$(prop 'app.storage.port'):9000 \     -h $(prop 'app.storage.host') \     -e STORAGE_ACCESS_KEY="$(prop 'app.storage.access-key')" \     -e STORAGE_SECRET_KEY="$(prop 'app.storage.secret-key')" \     -e STORAGE_BUCKET="$(prop 'app.storage.bucket')" \     -v "$(prop 'app.data-path')/storage":/app/storage \     myapp-storage:latest  docker create \     --name=myapp-database \     -p "$(prop 'app.database.address')":"$(prop 'app.database.port')":5432 \     -h "$(prop 'app.database.host')" \     -e POSTGRES_USER="$(prop 'app.database.user')" \     -e POSTGRES_PASSWORD="$(prop 'app.database.pass')" \     -e POSTGRES_DB="$(prop 'app.database.main')" \     -e PGDATA="/app/database" \     -v "$(prop 'app.data-path')/database":/app/database \     postgres:9.5 

File: env/dev.properties

app.data-path=/apps/myapp/  #========================================================== # Server properties #========================================================== app.server.address=127.0.0.70 app.server.host=dev.myapp.com app.server.port=8080  #========================================================== # Backend properties #========================================================== app.backend.address=127.0.0.70 app.backend.host=dev.myapp.com app.backend.port=8081 app.backend.maximum.threads=5  #========================================================== # Database properties #========================================================== app.database.address=127.0.0.70 app.database.host=database.myapp.com app.database.port=5432 app.database.user=dev-user-name app.database.pass=dev-password app.database.main=dev-database  #========================================================== # Storage properties #========================================================== app.storage.address=127.0.0.70 app.storage.host=storage.myapp.com app.storage.port=4569 app.storage.endpoint=http://storage.myapp.com:4569 app.storage.access-key=dev-access-key app.storage.secret-key=dev-secret-key app.storage.region=us-east-1 app.storage.bucket=dev-bucket 

Usage:

./setup.sh dev 
like image 93
Nicolai Avatar answered Sep 21 '22 20:09

Nicolai


As (Bourne) shell variables cannot contain dots you can replace them by underscores. Read every line, translate . in the key to _ and evaluate.

#/bin/sh  file="./app.properties"  if [ -f "$file" ] then   echo "$file found."    while IFS='=' read -r key value   do     key=$(echo $key | tr '.' '_')     eval ${key}=\${value}   done < "$file"    echo "User Id       = " ${db_uat_user}   echo "user password = " ${db_uat_passwd} else   echo "$file not found." fi 

Note that the above only translates . to _, if you have a more complex format you may want to use additional translations. I recently had to parse a full Ant properties file with lots of nasty characters, and there I had to use:

key=$(echo $key | tr .-/ _ | tr -cd 'A-Za-z0-9_') 
like image 35
fork2execve Avatar answered Sep 17 '22 20:09

fork2execve