Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
shell中从一个文件到另一个文件的变量引用组_Shell_Variables_Parameter Passing - Fatal编程技术网

shell中从一个文件到另一个文件的变量引用组

shell中从一个文件到另一个文件的变量引用组,shell,variables,parameter-passing,Shell,Variables,Parameter Passing,我甚至没有看到任何接近这个用例的东西,所以我不确定这在shell脚本中会是什么样子。基本上,我有一堆模板shell脚本,其中文件的唯一区别是一组4个变量。我想为这些变量创建另一个文件,如下所示: # Group 1 Var 1 = A Var 2 = B Var 3 = C Var 4 = D # Group 2 Var 1 = F Var 2 = G Var 3 = H Var 4 = I SITE="$1" VIP="Source ../conf/${$1.VIP}" DMN1="Sou

我甚至没有看到任何接近这个用例的东西,所以我不确定这在shell脚本中会是什么样子。基本上,我有一堆模板shell脚本,其中文件的唯一区别是一组4个变量。我想为这些变量创建另一个文件,如下所示:

# Group 1
Var 1 = A
Var 2 = B
Var 3 = C
Var 4 = D

# Group 2
Var 1 = F
Var 2 = G
Var 3 = H
Var 4 = I
SITE="$1"
VIP="Source ../conf/${$1.VIP}"
DMN1="Source ../conf/${$1.VAR1}"
DMN2="Source ../conf/${$1.VAR2}"
DMN3="Source ../conf/${$1.VAR3}"
然后我想使用一个shell脚本作为模板并传入一个参数。例如./script.sh group2。脚本将使用此参数,转到输入文件并确定脚本其余部分要使用的变量组。这可能吗?这个的正确语法是什么

更新:

我的想法是这样的:

# Group 1
Var 1 = A
Var 2 = B
Var 3 = C
Var 4 = D

# Group 2
Var 1 = F
Var 2 = G
Var 3 = H
Var 4 = I
SITE="$1"
VIP="Source ../conf/${$1.VIP}"
DMN1="Source ../conf/${$1.VAR1}"
DMN2="Source ../conf/${$1.VAR2}"
DMN3="Source ../conf/${$1.VAR3}"
这个想法是,当我运行脚本时,我运行./script.sh ABC
由此,脚本将使用ABC作为站点,然后从配置文件中,将标记为ABC.VIP的变量引用到ABC.DMN3。在配置文件中,它将读取ABC.VIP=“abcvip”。这不会缓解我的问题吗?我假设这样的东西会起作用,但我不确定它的语法。主要是链接到源变量引用

您可以通过一个简单的函数创建来实现,它间接地与上述情况相同

######### Make this as a source file #### 
group_1()
{
Var1=A
Var2=B
Var3=C
Var4=D
}

group_2()
{
Var1=F
Var2=G
Var3=H
Var4=I
}
###################
在一个文件中创建上述内容,然后创建另一个文件,在其中可以调用特定变量,如下所示

if [[ target == "group_1" ]]
  then
    group_1
    echo $Var1
  else
    group_2
  echo $Var1
fi

希望这能解决您的问题

如果您已经在不同的文件中定义了所需的变量组,那么您的脚本可以像普通配置文件一样为文件提供源。就像在脚本中添加“sourcegroup_1”。shWell这样做,我不会真正增加任何好处。我希望能够有一个变量配置文件和一个通用脚本,该脚本不包含参数,以从配置文件中选择要使用的组。