嵌套的shell变量不使用eval [英] nested shell variables without using eval
本文介绍了嵌套的shell变量不使用eval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能否摆脱评估
在这里?我试图设置 $ current_database
由用户输入(国家和行动)确定相应的变量
#用户输入
国家=ES
行动=销售#为current_database可能的变量
final_es_sales_path =blahblah / ES / sales.csv
final_en_support_path =yadayada / EN / support.csv
final_it_inventory_path =humhum /这/ inventory.csv
...current_database = $(EVAL回声\\ $ {_最终$ {}全国_ $ {}行动_path})
解决方案
您可以使用关联数组,加入这两个变量的值。例如:
声明-A数据库
#初始化
数据库[ES:销售] =blahblah / ES / sales.csv
数据库[恩:支持] =yadayada / EN / support.csv
然后,你就能通过获取数据库:
回声$ {数据库[$ {}国:$ {}行动]}
这具有仅由一个变量收集的数据库名称的优点。
Can I get rid of eval
here? I'm trying to set $current_database
with the appropriate variable determined by user input (country and action)
# User input
country="es"
action="sales"
# Possible variables for current_database
final_es_sales_path="blahblah/es/sales.csv"
final_en_support_path="yadayada/en/support.csv"
final_it_inventory_path="humhum/it/inventory.csv"
...
current_database=$(eval echo \${final_${country}_${action}_path})
解决方案
You can use associative arrays, joining the value of both variables. For example:
declare -A databases
# initialization
databases["es:sales"]="blahblah/es/sales.csv"
databases["en:support"]="yadayada/en/support.csv"
Then, you can get the database just by:
echo ${databases["${country}:${action}"]}
This has the advantage of having the database names collected by only one variable.
这篇关于嵌套的shell变量不使用eval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文