How to Read a .Properties File Which Contains Keys That Have a Period Character Using Shell Script

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

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_')

How to search a value inside a properties file with format key=value using sell script?

VAL=$(grep "$keyToSearch" "$myfile" | cut -d'=' -f2-)

The "-f2-" is basically asking for all the data after the first "=".

Check this: cut(1) - Linux man page

In your case:

myfile="./app.properties"
keyToSearch="EXAMPLE"
value=""
if [ -f "$myfile" ]
then
# echo "$myfile found." # no noise on success
#Search the keyToSearch and obtain the value.
value="$(grep "$keyToSearch" "$myfile" | cut -d'=' -f2-)"
else
echo "$myfile not found."
fi

Read .properties file to set value in use and password field in Expect shell script

If you can modify the format of the properties file then the easiest way would be to use the source functionality of tcl (expect script are practically tcl scripts).

!/usr/bin/expect
source values.prop
spawn ssh $user@$host
expect "$user@$hosts's password:"
send "$password\n"

With values.prop now looking like this:

set user "abcd"
set password "xxxx"

Here is a crude shell script which can automatically translate the properties file to a tcl file

#!/bin/sh
. "$1"
printf 'set user "%s"\n' "$user"
printf 'set password "%s"\n' "$password"

Use like this:

translate ./values.prop > values.prop.tcl

Then source values.prop.tcl in your expect script.

Bash: How to parse a log file that contains the output of ls -ltr using bash script to extract the file names that are modified before specific time

I ended up parsing the log file line by line, extracted the last modified time to a variable and file name to another variable. Then I converted the last modified time string to Unix time format and then to epoch number. Similarly I got the epoch number for sysdate-1. Finally I compared those two values and filtered the file names matched my criteria. code snippet is given below

prev_day_time=$(date +%s -d "$(date --date='1 day ago')")
file_mod_time=$(date +%s -d "$(date -d "$(echo $(echo $files_list | awk '{print $1}') | sed -e 's/\(.....\)/\1 /')")")

dynamically fetching dynamic variable's value from properties file

Using indirection is far safer than eval:

#!/bin/bash
. $MYDIR/myProperties.properties # myTempVar1=myTempVar2
myTempVar=myTempVar1
myTempVar3=${!myTempVar}
echo $myTempVar3

Gives:

myTempVar2

and you don't need the echo in eval:

eval myTempVar3='$'$myTempVar

shell script to read filename from a text file line by line and to confirm they are present in two different directories

I've created a test directories and files to show how it can be done.

Create directories dir1 anddir2`

mkdir -p dir{1..2}

Check the directories.

ls 

dir1 dir2

Create the files.

touch dir{1..2}/{1..10}.txt

Check the files.

ls dir{1..2}/
dir1/:
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt

dir2/:
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt

Creat the contents of the file.

printf '%s\n' {1..10}.txt > list_of_files.txt
printf '%s\n' {a..c} >> list_of_files.txt

Check the contents of the file.

cat list_of_files.txt

The output is

1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
10.txt
a
b
c

The variable foo is dir1 and the variable bar is dir2 in the script.

#!/bin/sh

foo=dir1
bar=dir2

while read -r files_in_text; do
if [ ! -e "$foo"/"$files_in_text" ] && [ ! -e "$bar"/"$files_in_text" ]; then
printf 'files %s and %s does not exists!\n' "$foo"/"$files_in_text" "$bar"/"$files_in_text"
else
printf 'files %s and %s does exists.\n' "$foo"/"$files_in_text" "$bar"/"$files_in_text"
fi
done < list_of_files.txt

The output should be

files dir1/1.txt and dir2/1.txt does exists.
files dir1/2.txt and dir2/2.txt does exists.
files dir1/3.txt and dir2/3.txt does exists.
files dir1/4.txt and dir2/4.txt does exists.
files dir1/5.txt and dir2/5.txt does exists.
files dir1/6.txt and dir2/6.txt does exists.
files dir1/7.txt and dir2/7.txt does exists.
files dir1/8.txt and dir2/8.txt does exists.
files dir1/9.txt and dir2/9.txt does exists.
files dir1/10.txt and dir2/10.txt does exists.
files dir1/a and dir2/a does not exists!
files dir1/b and dir2/b does not exists!
files dir1/c and dir2/c does not exists!


Related Topics



Leave a reply



Submit