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

发布于 2024-11-04 05:36:17 字数 333 浏览 0 评论 0原文

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

当前目录是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

万人眼中万个我 2024-11-11 05:36:17

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.

欢烬 2024-11-11 05:36:17

ondir 程序可让您指定在终端中进入和离开目录时要运行的操作

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

陈甜 2024-11-11 05:36:17

我已经编写了另一个实现,它与 ondir 有点相似。当我开始研究 ondir 时,我实际上并不知道它。然而,有一些可能有用的关键差异。

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

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

  • 它具有“变量存储”,这是处理环境的更自动的方式变量,而 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

樱花坊 2024-11-11 05:36:17

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.

巷子口的你 2024-11-11 05:36:17

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

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.

风为裳 2024-11-11 05:36:17

这不太漂亮,但您可以结合使用导出的环境变量和 $PWD 的值。

例如:

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

那么 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:

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

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

耀眼的星火 2024-11-11 05:36:17

如何用一个函数包装您的脚本(该函数可以放置在系统文件中的 bash 配置文件/bashrc 文件中,以供所有用户使用)。

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

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

以此为例:

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

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 ).

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.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文