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
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.
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.
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.
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
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_')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With