Reputation: 29816
I have following code:
#!/bin/bash
PROP_FILE="${HOME}/my-prop.properties"
getProperty() {
echo `cat ${PROP_FILE} | grep ${1} | cut -d'=' -f2`
}
PORT=$(getProperty "PORT")
HOSTS=$(getProperty "HOSTS")
#PORT=myPort
#HOSTS=myHosts
echo ${PORT}
echo ${HOSTS}
EXE="${HOME}/server/bin/my-executable.sh"
bash ${EXE} start -p ${PORT} -h ${HOSTS}
Now when I am running this shell script it is giving me an error: expr: non-numeric argument
. If I hard-code the value (which has shown as the commented code), it works.
Following is the my-prop.properties:
PORT=myPort
HOSTS=myHosts
Any help would be much appreciated.
Upvotes: 1
Views: 445
Reputation: 85825
If there are multiple such key value pairs in the file and want to filter only PORT
and HOST
do a process-substitution as below
#!/usr/bin/env bash
propFile="${HOME}/my-prop.properties"
scriptPath="${HOME}/server/bin/my-executable.sh"
while read -r port; read -r host; do
bash "$scriptPath" start -p "$port" -h "$host"
done < <(awk -F= '$1=="PORT" || $1=="HOSTS"{print $2}' "$propFile")
The below would work only if there are those two key-value pairs in the properties file.
Why the round about way to achieve such a simple task. You can do this in bash
with input re-direction on the properties file with setting IFS
on =
and just read the second word containing the host and port values.
#!/usr/bin/env bash
propFile="${HOME}/my-prop.properties"
scriptPath="${HOME}/server/bin/my-executable.sh"
while IFS== read -r _ port; IFS== read -r _ host; do
bash "$scriptPath" start -p "$port" -h "$host"
done < "$propFile"
This works under an assumption that you have only the two lines in the properties file as shown. If needed you can print the values of host and port before calling the actual script.
Upvotes: 1