Unix,糟糕的替换错误?

时间:2018-03-07 05:18:10

标签: linux bash shell unix

我有以下变量:

-bash: ${STAT_${TYP}} : bad substitution 

但是我收到了一个错误:

plot <- ggplot(melt_train2, aes(x=f,label=f)) + big_theme + theme2 + theme3

plot + labs(y="abc") +

geom_col(position = "identity",aes(x=d,y=v,width=0.5,

fill=forcats::fct_relevel(variable, c("a", "b","c", "d")),

color=forcats::fct_relevel(variable, c("a", "b","c", "d"))))+

 scale_alpha(c(0,0,1,1),labels=c("a","b","c","d"))  +

 scale_fill_manual(values=c("#000000","#000000","#CD572E","#275636")

,labels=c("a","b","c","d")) + 

scale_color_manual(values=c("#EE82EE","#4782A6","#CD572E","#275636")

,labels=c("a","b","c","d")) + labs(y="abc") +

facet_grid(.~grp2,space="free_x", scales="free_x", 

switch="x",shrink=FALSE)  + 

geom_hline(yintercept=1,color="white",size=2)+

theme(strip.text=element_text(size=18,hjust = 0.5),strip.placement = "outside",
strip.background = element_rect( colour="white"),

panel.spacing=unit(0,"cm")) + coord_cartesian(ylim=c(minval,maxval))

+scale_y_continuous(breaks=seq(minval,maxval,(maxval-minval)/5))

我想要的是,FINAL_VARIABLE的值是'统计'

请帮助..

3 个答案:

答案 0 :(得分:1)

你不能像那样嵌套变量扩展。但您可以使用${!varname}

进行间接变量扩展
TYP=a1
STAT_a1=statistical
STAT_var=STAT_${TYP}    # This sets STAT_var to "STAT_a1"
FINAL_VARIABLE=${!STAT_var}    # This sets FINAL_VARIABLE to "statistical"

顺便说一句,我建议避免使用诸如TYPFINAL_VARIABLE之类的全大写变量名称 - 有许多全大写字母名称对shell和/或其他程序有特殊含义,如果你不小心使用了其中一种奇怪的事情。

答案 1 :(得分:0)

使用eval,将您想要推迟评估的所有内容包装在单引号中:

eval 'FINAL_VARIABLE=${STAT_'${TYP}'}'

$ TYPE=al
$ STAT_a1=statistical
$ eval 'FINAL_VARIABLE=${STAT_'${TYP}'}'
$ echo $FINAL_VARIABLE
statistical

答案 2 :(得分:-1)

您也可以这样做

root@myagent: tmp$ F=$(echo "${STAT_a1}"_"${TYP}")
root@myagent: tmp$ echo $F
statistical_a1