如何使用shell脚本中的输入查找参数值

时间:2016-01-27 14:35:10

标签: shell

我有一个场景来创建一个通用脚本,该脚本将使用输入值并从配置文件中获取实际值,并将其用于进一步的逻辑。

pattern.config file
TYPE1_PATH=/path/to/type1
TYPE2_PATH=/path/to/type2

我想运行我的脚本./run.sh TYPE1并且现在PATTERN=$1"_PATH" $PATTERN=TYPE1_PATH。但不确定如何从配置

获取$TYPE1_PATH的值

1 个答案:

答案 0 :(得分:1)

这是Bash FAQ 006

具体来说Evaluating indirect/reference variables

# Bash
realvariable=contents
ref=realvariable
echo "${!ref}"   # prints the contents of the real variable

# ksh93 / mksh / Bash 4.3
realvariable=contents
typeset -n ref=realvariable
echo "${!ref} = $ref"      # prints the name and contents of the real variable

# zsh
realvariable=contents
ref=realvariable
echo ${(P)ref}   # prints the contents of the real variable
相关问题