基于目录的环境变量范围-如何实现?
我有一组工具,需要根据我正在处理的项目传递参数。我希望能够根据当前目录自动设置几个环境变量。因此,当我在目录之间切换时,我常用的环境变量也会改变。示例:
当前目录是 foo,因此如果我这样做:
~/foo$ ./myscript --var1=$VAR1
VAR1 将具有一些基于 foo 的值。
然后,假设我切换到 bar 目录。如果我这样做:
~/bar$ ./myscript --var1=$VAR1
VAR1 现在应该有一些基于柱的值。
这可能吗?如何?
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$ ./myscript --var1=$VAR1
VAR1 would have some foo based value.
Then, let's say I switched to bar directory. If I do:
~/bar$ ./myscript --var1=$VAR1
VAR1 should now have some bar based value.
Is that possible? How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
direnv 可以帮助您轻松、优雅地完成这些工作。只需在项目目录中定义一个 .envrc 文件,其中包含所需的所有环境变量,一旦您 cd 进入该文件夹,它将获取它。
There is direnv which 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.
ondir 程序可让您指定在终端中进入和离开目录时要运行的操作
the ondir program lets you specify actions to run when you enter and leave directories in a terminal
我已经编写了另一个实现,它与 ondir 有点相似。当我开始研究 ondir 时,我实际上并不知道它。然而,有一些可能有用的关键差异。
smartcd 完全用 shell 编写,并且完全兼容 bash 和 zsh,甚至更深奥的选项
,
它具有“变量存储”,这是处理环境的更自动的方式变量,而 ondir 要求您显式手动删除和/或重置变量
smartcd 可以通过挂接提示命令(bash 中的 PROMPT_COMMAND,zsh 中的 precmd)来打开“autocd”。
您可以在 https://github.com/cxreg/smartcd
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.
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)
You can find smartcd at https://github.com/cxreg/smartcd
bash 或任何其他常见 shell 的内置功能不直接支持这一点。但是,您可以创建自己的“cd”命令来执行您想要的任何操作。例如,您可以使用别名 cd 来执行 cd,然后运行特殊脚本(例如:~/bin/oncd)。该脚本可以在数据库中查找新目录并运行一些命令,或者查看目录中是否有特殊文件(例如:.env)并加载它,等等。
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.
我经常做这种事。我在需要的目录中创建了几个相同名称的批处理文件,这些文件仅设置变量并调用通用脚本。我什至有一个批处理文件来创建其他小文件。
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.
这不太漂亮,但您可以结合使用导出的环境变量和
$PWD
的值。例如:
那么
myscript
只需要eval echo \${$VAR1${PWD////_}}
即可获取基于目录的值。This is not pretty, but you can use a combination of exported environment variables and the value of
$PWD
.For example:
Then
myscript
needs only toeval echo \${$VAR1${PWD////_}}
to get at the directory based value.如何用一个函数包装您的脚本(该函数可以放置在系统文件中的 bash 配置文件/bashrc 文件中,以供所有用户使用)。
因此,函数 myscript 将调用真正的“myscript”,知道根据当前工作目录要做什么。
以此为例:
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 ).
Hence the function myscript will call the real "myscript" knowing what to do based on the current working directory.
Take this as an example: