#!/bin/bash
expand_env(){
to_eval=$*
final_value=$(eval echo $to_eval)
until [ "$final_value" = "$to_eval" ]
do
to_eval=$final_value
final_value=$(eval echo $to_eval)
done
echo $final_value
}
echo $applicationname
echo $HOSTNAME
echo $CUSTOM_OPTS
EXPANDED_CUSTOM_OPTS=`expand_env $CUSTOM_OPTS`
echo $EXPANDED_CUSTOM_OPTS
输出如下:
world-guyq
gisapp-world-guyq-644599d4bc-qv8ns
-XX:OnError=/opt/sendcrashalert.sh -XX:ErrorFile=/data/jvm_logs/$applicationname.$HOSTNAME.hslog_hs_err_pid_%p.log
-XX:OnError=/opt/sendcrashalert.sh -XX:ErrorFile=/data/jvm_logs/world-guyq.gisapp-world-guyq-644599d4bc-qv8ns.hslog_hs_err_pid_%p.log
可见环境变量CUSTOM_OPTS中嵌套的applicationname和HOSTNAME已经展开