nested shell variables without using eval

后端 未结 3 1655
慢半拍i
慢半拍i 2021-01-12 01:42

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)

3条回答
  •  再見小時候
    2021-01-12 02:28

    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.

提交回复
热议问题