Directory based environment variable scope - how t

2019-03-14 11:27发布

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?

7条回答
该账号已被封号
2楼-- · 2019-03-14 11:52

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.

查看更多
做个烂人
3楼-- · 2019-03-14 11:55

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

查看更多
劫难
4楼-- · 2019-03-14 11:56

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.

查看更多
Luminary・发光体
5楼-- · 2019-03-14 11:57

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

查看更多
小情绪 Triste *
6楼-- · 2019-03-14 11:58

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
查看更多
smile是对你的礼貌
7楼-- · 2019-03-14 12:04

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.

查看更多
登录 后发表回答