Reputation: 83
I have a table which contains an env variable and I need to fetch the variable's value and export for another java utility from within the shell script:
command="SELECT param_value FROM tableX WHERE param_name='ABCD';"
#This param_value is ${PATHX} and PATHX is /home/users/pathx
PARAM_VALUE=`sqlplus -s $CONN_STRING <<- END
SET head off;
set feedback off;
${command}
exit;
END`
echo ${PARAM_VALUE} | grep -q "ERROR"
if [ $? -eq 0 ]
then
echo "Failed in fetching param_value "
exit 1
else
#Trimming the value fetched from DB
PARAM_VALUE=`echo "${PARAM_VALUE}" | tr -d " " | tr -d "\n"`
echo "Value fetched from DB=${PARAM_VALUE}"
#This prints ${PATHX}
export PATH_VALUE="${PARAM_VALUE}"
#This is exporting PATH_VALUE as ${PATHX} instead of /home/users/pathx - WHICH IS WHERE I NEED HELP
#If I put directly export PATH_VALUE="${PATHX}", it exports the value correctly as /home/users/pathx
fi
After searching for options, I have tried various options like below but failed:
export PATH_VALUE="${PARAM_VALUE}"
export PATH_VALUE=`eval echo "\$${PARAM_VALUE}"`
export PATH_VALUE=$(eval \$$PARAM_VALUE)
export PATH_VALUE=${$PARAM_VALUE}
export PATH_VALUE=${!PARAM_VALUE}
export PATH_VALUE=`echo ${PARAM_VALUE}`
export PATH_VALUE=`expr ${PARAM_VALUE}`
Please suggest what can be done in this case to export the actual expanded value - /home/users/pathx.
Upvotes: 0
Views: 221
Reputation: 141020
yes the query response is ${PATHX} and this is an environment variable which I need to expand.
You could use eval
, but eval
is evil, if the value is exported, do a safe envsubst
:
PARAM_VALUE=$(envsubst <<<"$PARAM_VALUE")
Upvotes: 1
Reputation: 714
For it to work the way you expect, the sqlplus query response should be PATHX
instead of ${PATHX}
. One way to fix it is to replace
PARAM_VALUE=`echo "${PARAM_VALUE}" | tr -d " " | tr -d "\n"`
with
PARAM_VALUE=`echo "${PARAM_VALUE}" | tr -dc '[:alnum:]\n\r'`
Upvotes: 2