Linux environment variables referencing other variables

谁说胖子不能爱 提交于 2019-12-11 05:54:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/37940698/linux-environment-variables-referencing-other-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!