shell 变量的双重扩展
Double expansion of shell variable
我有一个 table,其中包含一个环境变量,我需要获取该变量的值并从 shell 脚本中导出另一个 java 实用程序:
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
搜索选项后,我尝试了如下各种选项但都失败了:
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}`
请建议在这种情况下如何导出实际扩展值 - /home/users/pathx。
为了按您期望的方式工作,sqlplus 查询响应应该是 PATHX
而不是 ${PATHX}
。修复它的一种方法是替换
PARAM_VALUE=`echo "${PARAM_VALUE}" | tr -d " " | tr -d "\n"`
和
PARAM_VALUE=`echo "${PARAM_VALUE}" | tr -dc '[:alnum:]\n\r'`
yes the query response is ${PATHX} and this is an environment variable which I need to expand.
你可以使用eval
,但是eval
是邪恶的,如果值被导出,做一个安全的envsubst
:
PARAM_VALUE=$(envsubst <<<"$PARAM_VALUE")
我有一个 table,其中包含一个环境变量,我需要获取该变量的值并从 shell 脚本中导出另一个 java 实用程序:
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
搜索选项后,我尝试了如下各种选项但都失败了:
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}`
请建议在这种情况下如何导出实际扩展值 - /home/users/pathx。
为了按您期望的方式工作,sqlplus 查询响应应该是 PATHX
而不是 ${PATHX}
。修复它的一种方法是替换
PARAM_VALUE=`echo "${PARAM_VALUE}" | tr -d " " | tr -d "\n"`
和
PARAM_VALUE=`echo "${PARAM_VALUE}" | tr -dc '[:alnum:]\n\r'`
yes the query response is ${PATHX} and this is an environment variable which I need to expand.
你可以使用eval
,但是eval
是邪恶的,如果值被导出,做一个安全的envsubst
:
PARAM_VALUE=$(envsubst <<<"$PARAM_VALUE")