bash 基于目录的环境变量范围 - 如何实现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5848832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:53:38  来源:igfitidea点击:

Directory based environment variable scope - how to implement?

bashscopeenvironment-variablesworking-directory

提问by Igor Gatis

I have a set of tools which I need to pass parameters depending on the project I'm working on. I'd like to be able to automatically set a couple of environment variables based on the current directory. So when I switched between directories, my commonly used env vars would also change. Example:

我有一组工具,我需要根据我正在处理的项目传递参数。我希望能够根据当前目录自动设置几个环境变量。所以当我在目录之间切换时,我常用的环境变量也会改变。例子:

Let's current directory is foo, thus if I do:

让我们的当前目录是 foo,因此如果我这样做:

~/foo$ ./myscript --var1=$VAR1

VAR1 would have some foo based value.

VAR1 会有一些基于 foo 的值。

Then, let's say I switched to bar directory. If I do:

然后,假设我切换到 bar 目录。如果我做:

~/bar$ ./myscript --var1=$VAR1

VAR1 should now have some bar based value.

VAR1 现在应该有一些基于柱的值。

Is that possible? How?

那可能吗?如何?

采纳答案by Dan D.

the ondirprogram lets you specify actions to run when you enter and leave directories in a terminal

ondir程序允许您指定的动作时运行在终端进入和离开目录

回答by sreenivas

There is direnvwhich helps you do this stuff much easily and in an elegant way. Just define a .envrc file in your project directory with all the env variables needed and it will source it once you cd into that folder.

direnv它可以帮助你做到这一点的东西太多轻松和优雅的方式。只需在您的项目目录中定义一个 .envrc 文件,其中包含所需的所有 env 变量,一旦您 cd 进入该文件夹,它就会获取它。

回答by cxreg

I've written another implementation of this, which is somewhat similar to ondir. I didn't actually know about ondir when I started working on it. There are some key differences that may be useful, however.

我已经写了另一个实现,它有点类似于 ondir。当我开始研究 ondir 时,我实际上并不了解它。但是,有一些可能有用的关键差异。

  • smartcd is written entirely in shell, and is fully compatible with bash and zsh, even the more esoteric options

  • smartcd will run scripts all the way down and up the directory hierarchy down to their common ancestor, not just for the two directories you're entering and leaving. This means you can have a ~/foo script that will execute whether you "cd ~/foo" or "cd ~/foo/bar"

  • it has "variable stashing" which is a more automatic way of dealing with your environment variables, whereas ondir requires you to explicitly and manually remove and/or reset your variables

  • smartcd can work with "autocd" turned on by hooking your prompt command (PROMPT_COMMAND in bash, precmd in zsh)

  • smartcd 完全用 shell 编写,并且与 bash 和 zsh 完全兼容,甚至是更深奥的选项

  • smartcd 将在目录层次结构中一直向下和向上运行脚本,直到它们的共同祖先,而不仅仅是您进入和离开的两个目录。这意味着你可以有一个 ~/foo 脚本,无论你是“cd ~/foo”还是“cd ~/foo/bar”,它都会执行

  • 它具有“变量存储”,这是一种更自动的处理环境变量的方式,而 ondir 要求您显式和手动删除和/或重置您的变量

  • smartcd 可以通过挂钩您的提示命令(bash 中的 PROMPT_COMMAND,zsh 中的 precmd)打开“autocd”

You can find smartcd at https://github.com/cxreg/smartcd

你可以在https://github.com/cxreg/smartcd找到 smartcd

回答by Bryan Oakley

This is not something that is directly supported with the built-in features of bash or any other common shell. However, you can create your own "cd" command that will do whatever you want. For example, you could alias cd to do the cd and then run a special script (eg: ~/bin/oncd). That script could look up the new directory in a database and run some commands, or see if there's a special file (eg: .env) in the directory and load it, etc.

这不是 bash 或任何其他常见 shell 的内置功能直接支持的东西。但是,您可以创建自己的“cd”命令来执行您想要的任何操作。例如,您可以使用别名 cd 来执行 cd,然后运行一个特殊的脚本(例如:~/bin/oncd)。该脚本可以在数据库中查找新目录并运行一些命令,或者查看目录中是否存在特殊文件(例如:.env)并加载它等。

回答by Chen Levy

This is not pretty, but you can use a combination of exported environment variables and the value of $PWD.

这并不漂亮,但您可以使用导出的环境变量和$PWD.

For example:

例如:

export VAR1=prefix
export prefix${HOME////_}_foo=42
export prefix${HOME////_}_bar=blah

Then myscriptneeds only to eval echo \${$VAR1${PWD////_}}to get at the directory based value.

然后myscript只需要eval echo \${$VAR1${PWD////_}}获取基于目录的值。

回答by hmontoliu

How about wrap your script with a function (the function can be placed either in your bash profile/bashrc file in the system ones to make available for all the users ).

用一个函数包装你的脚本怎么样(这个函数可以放在你的 bash 配置文件/系统文件中的 bashrc 文件中,以便所有用户都可以使用)。

myscript () { case $PWD in
/path/to/foo) path/to/myscript --var1=$VAR1 ;;
/path/to/bar) path/to/myscript --var2=$VAR1 ;;
*) ;;
case
}

Hence the function myscript will call the real "myscript" knowing what to do based on the current working directory.

因此,函数 myscript 将根据当前工作目录调用真正的“myscript”,知道要做什么。

Take this as an example:

以此为例:

hmontoliu@ulises:/tmp$ myscript () { case $PWD in /tmp) echo I\'m in tmp;; /var) echo I\'m in var;; *) echo I\'m neither in tmp nor in bar; esac; }
hmontoliu@ulises:/tmp$ myscript 
I'm in tmp
hmontoliu@ulises:/tmp$ cd /var
hmontoliu@ulises:/var$ myscript 
I'm in var
hmontoliu@ulises:/var$ cd /etc
hmontoliu@ulises:/etc$ myscript 
I'm neither in tmp nor in bar

回答by IronMensan

I do this sort of thing a lot. I create several identically named batch files in directories where I need them that only set the variables and call the common script. I even have a batch file that creates the other small files.

我经常做这种事情。我在我需要的目录中创建了几个同名的批处理文件,这些文件只设置变量并调用通用脚本。我什至有一个创建其他小文件的批处理文件。