动态环境变量在Linux中? [英] Dynamic environment variables in Linux?

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

问题描述



我有一个网站服务器,其中网站遵循这种布局:

 网站/ 
qa /
生产/

当我进入qa目录时,我想要一个设置为qa的环境变量(例如APPLICATION_ENV),当我在生产目录。这样做的原因是,各种网站可以使用许多不同的流程,所有流程都需要知道是否是质量检查或生产环境。一些网站在Apache下使用PHP,一些使用Node.js,一些站点附带命令行工具,cron作业等。我想要一个有关QA或生产环境的知识来源。

解决方案

根据 rvm 的覆盖:

  cd(){
如果内置cd$ @
然后
如果[[$ PWD=〜/(qa | production) (/ | $)]]
然后
export APPLICATION_ENV =$ {BASH_REMATCH [1]}
else
unset APPLICATION_ENV
fi
return 0
else
返回$?
fi
}

只需将此函数放在 .bashrc 或其他一些来源的环境文件,并尝试将 cd 转换为 qa production ,或其子目录之一。


Is it possible in some way to have dynamic environment variables in Linux?

I have a webserver where sites follow this layout:

site/
    qa/
    production/

I would like to have an environment variable (e.g. APPLICATION_ENV) that is set to "qa" when I'm in the qa directory, and to "production" when I'm in the production directory. The reason for this is that various sites can use many different processes that all need to know if it's the QA or production environment. Some sites use PHP under Apache, some use Node.js, some sites come with commandline tools, cron jobs, etcetera. I would like to have one authorative source on whether it's a QA or a production environment.

解决方案

Based on rvm's override:

cd () { 
    if builtin cd "$@"
    then
        if [[ "$PWD" =~ /(qa|production)(/|$) ]]
        then
            export APPLICATION_ENV="${BASH_REMATCH[1]}"
        else
            unset APPLICATION_ENV
        fi
        return 0
    else
        return $?
    fi
}

Just put this function in .bashrc or some other sourced environment file, and try to cd into qa, production, or one of their subdirectories.

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

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