Directory based environment variable scope - how to implement?

前端 未结 7 510
庸人自扰
庸人自扰 2021-02-04 00:03

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

7条回答
  •  情歌与酒
    2021-02-04 00:34

    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
    

提交回复
热议问题