Linux环境变量引用其他变量 [英] Linux environment variables referencing other variables

查看:618
本文介绍了Linux环境变量引用其他变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用网络供应商的引导脚本,我在RedHat 7.2上使用这个脚本。起始脚本使用几个变量来设置环境,但是我认为这些变量的设置不正确。



我已经将启动脚本添加到/ etc / environment中,我可以看到所有用户都定义了可用的变量。



这是脚本中变量定义的一个例子:

  export V1 = / opt / nameofsupplier / sdk / CentOS-RHEL-7-x86_64 
export V2 = $ {V1} / lib / cam

如果我从终端尝试这样的话,还有更多的东西:

  cd $ V1 

它工作正常,但是如果我尝试:

 code> cd $ V2 

我得到:

  base:cd $ V1 / lib / cam:没有这样的文件或目录

路径是有效的,如果我在shell中执行此操作:

  export V2 = $ {V1} / lib / cam 
cd $ V2

它没有任何错误,如何修复脚本?

解决方案

您可能会怀疑这些变量的不明确性。
/ etc / environment 只能包含变量定义 - 它不像普通脚本一样执行(请参阅其文档这里,其中变量扩展在/ etc / environment中不起作用。),所以在V2的定义发生。因此,V2未正确定义。


尝试在系统范围内$ 中的 / etc / environment etc / profile (或其等同,取决于用户的shell)或特定用户的〜/ .profile s。

作为最后的手段,您可以将 / etc / environment 各自的行复制到上述脚本中(但这将使维护更难)。 BR>

您还可以更正 / etc / environment 中的定义,而不是依赖扩展,即:

  export V2 = / opt / nameofsupplier / sdk / CentOS-RHEL-7-x86_64 / lib / cam 
/ pre>

(假设没有太多的要更正)。但这也很难维护。


I am using a boot script from a network vendor, I am using this on RedHat 7.2 The start script sets up the environment with several variables, however I don't think these variables are set-up correctly.

I have added the start-up script to /etc/environment and I can see that the variables are defined and available to all users.

This is an example of how the variables are defined in the script:

    export V1=/opt/nameofsupplier/sdk/CentOS-RHEL-7-x86_64
    export V2=${V1}/lib/cam

There are many more, if I try this from a terminal:

    cd $V1

It works fine, however if I try:

    cd $V2

I get:

    base: cd $V1/lib/cam: No such file or directory

The path is valid, and if I do this in the shell:

    export V2=${V1}/lib/cam
    cd $V2

It works without any error, how do I fix the script?

解决方案

You may be right in suspecting an ill-definition of these variables. /etc/environment can only contain variable definitions - it is not executed like a normal script (see its documentation here, which says Variable expansion does not work in /etc/environment.), so no variable expansion of V1 in the definition of V2 takes place. Therefore V2 is not correctly defined.

Try to source /etc/environment lines in the system-wide /etc/profile (or its equivalent, depending on the shells of your users) or in specific users' ~/.profiles.

As the last resort you can just plain copy the respective lines of /etc/environment to the above mentioned scripts (but this will make it harder to maintain).

You could also correct the definitions in /etc/environment not to rely on expansion, i.e. like this:

export V2=/opt/nameofsupplier/sdk/CentOS-RHEL-7-x86_64/lib/cam

(assuming there are not too much of them to be corrected). But this will also be hard to maintain.

这篇关于Linux环境变量引用其他变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆