在`cd`命令上运行bash脚本

时间:2020-12-17 01:13:32

I'm python developer and most frequently I use buildout for managing my projects. In this case I dont ever need to run any command to activate my dependencies environment.

我是python开发人员,最常使用buildout来管理我的项目。在这种情况下,我不需要运行任何命令来激活我的依赖项环境。

However, sometime I use virtualenv when buildout is to complicated for this particular case.

但是,有时我使用virtualenv当buildout对于这个特殊情况来说很复杂。

Recently I started playing with ruby. And noticed very useful feature. Enviourement is changing automatically when I cd in to the project folder. It is somehow related to rvm nad .rvmrc file.

最近我开始玩红宝石。并注意到非常有用的功能。当我进入项目文件夹时,环境会自动改变。它与rvm nad .rvmrc文件有某种关联。

I'm just wondering if there are ways to hook some script on different bash commands. So than I can workon environment_name automatically when cd into to project folder.

我只是想知道是否有办法在不同的bash命令上挂钩一些脚本。因此,当cd进入项目文件夹时,我可以自动workon environment_name。

So the logic as simple as:

所以逻辑很简单:

When you cd in the project with folder_name, than script should run workon folder_name

使用folder_name在项目中进行cd时,脚本应该运行workon folder_name

3 个解决方案

#1


10  

One feature of Unix shells is that they let you create shell functions, which are much like functions in other languages; they are essentially named groups of commands. For example, you can write a function named mycd that first runs cd, and then runs other commands:

Unix shell的一个特性是它们可以让你创建shell函数,这与其他语言中的函数非常相似;它们基本上是命名的命令组。例如,您可以编写一个名为mycd的函数,该函数首先运行cd,然后运行其他命令:

function mycd () {
    cd "$@"
    if ... ; then
        workon environment
    fi
}

(The "$@" expands to the arguments that you passed to mycd; so mycd /path/to/dir will call cd /path/to/dir.)

(“$ @”扩展为传递给mycd的参数;因此mycd / path / to / dir将调用cd / path / to / dir。)

As a special case, a shell function actually supersedes a like-named builtin command; so if you name your function cd, it will be run instead of the cd builtin whenever you run cd. In that case, in order for the function to call the builtin cd to perform the actual directory-change (instead of calling itself, causing infinite recursion), it can use Bash's builtin builtin to call a specified builtin command. So:

作为一种特殊情况,shell函数实际上取代了类似命名的内置命令;因此,如果您将功能命名为cd,则只要运行cd,就会运行cd而不是cd builtin。在这种情况下,为了使函数调用内置cd来执行实际的目录更改(而不是调用自身,导致无限递归),它可以使用Bash的内置内置来调用指定的内置命令。所以:

function cd () {
    builtin cd "$@"    # perform the actual cd
    if ... ; then
        workon environment
    fi
}

(Note: I don't know what your logic is for recognizing a project directory, so I left that as ... for you to fill in. If you describe your logic in a comment, I'll edit accordingly.)

(注意:我不知道你的识别项目目录的逻辑是什么,所以我把它留作......让你填写。如果你在评论中描述你的逻辑,我会相应地编辑。)

#2


2  

I think you're looking for one of two things.

我想你正在寻找两件事之一。

autoenv is a relatively simple tool that creates the relevant bash functions for you. It's essentially doing what ruakh suggested, but you can use it without having to know how the shell works.

autoenv是一个相对简单的工具,可以为您创建相关的bash函数。它基本上是做ruakh建议的,但你可以使用它而不必知道shell是如何工作的。

virtualenvwrapper is full of tools that make it easier to build smarter versions of the bash functions—e.g., switch to the venv even if you cd into one of its subdirectories instead of the base, or track venvs stored in git or hg, or … See the Tips and Tricks page.

virtualenvwrapper充满了工具,可以更容易地构建更智能的bash功能版本 - 例如,即使你进入其子目录而不是基地,也可以切换到venv,或者跟踪存储在git或hg中的venv,或者......提示和技巧页面。

The Cookbook for autoenv, shows some nifty ways ways to use the two together.

关于autoenv的Cookbook,展示了一些使用这两者的方法。

#3


0  

Just found in the description of virtualenvwraper this topic

刚刚在virtualenvwraper这个主题的描述中找到了

It describes exactly what I need.

它准确描述了我的需求。

#1


10  

One feature of Unix shells is that they let you create shell functions, which are much like functions in other languages; they are essentially named groups of commands. For example, you can write a function named mycd that first runs cd, and then runs other commands:

Unix shell的一个特性是它们可以让你创建shell函数,这与其他语言中的函数非常相似;它们基本上是命名的命令组。例如,您可以编写一个名为mycd的函数,该函数首先运行cd,然后运行其他命令:

function mycd () {
    cd "$@"
    if ... ; then
        workon environment
    fi
}

(The "$@" expands to the arguments that you passed to mycd; so mycd /path/to/dir will call cd /path/to/dir.)

(“$ @”扩展为传递给mycd的参数;因此mycd / path / to / dir将调用cd / path / to / dir。)

As a special case, a shell function actually supersedes a like-named builtin command; so if you name your function cd, it will be run instead of the cd builtin whenever you run cd. In that case, in order for the function to call the builtin cd to perform the actual directory-change (instead of calling itself, causing infinite recursion), it can use Bash's builtin builtin to call a specified builtin command. So:

作为一种特殊情况,shell函数实际上取代了类似命名的内置命令;因此,如果您将功能命名为cd,则只要运行cd,就会运行cd而不是cd builtin。在这种情况下,为了使函数调用内置cd来执行实际的目录更改(而不是调用自身,导致无限递归),它可以使用Bash的内置内置来调用指定的内置命令。所以:

function cd () {
    builtin cd "$@"    # perform the actual cd
    if ... ; then
        workon environment
    fi
}

(Note: I don't know what your logic is for recognizing a project directory, so I left that as ... for you to fill in. If you describe your logic in a comment, I'll edit accordingly.)

(注意:我不知道你的识别项目目录的逻辑是什么,所以我把它留作......让你填写。如果你在评论中描述你的逻辑,我会相应地编辑。)

#2


2  

I think you're looking for one of two things.

我想你正在寻找两件事之一。

autoenv is a relatively simple tool that creates the relevant bash functions for you. It's essentially doing what ruakh suggested, but you can use it without having to know how the shell works.

autoenv是一个相对简单的工具,可以为您创建相关的bash函数。它基本上是做ruakh建议的,但你可以使用它而不必知道shell是如何工作的。

virtualenvwrapper is full of tools that make it easier to build smarter versions of the bash functions—e.g., switch to the venv even if you cd into one of its subdirectories instead of the base, or track venvs stored in git or hg, or … See the Tips and Tricks page.

virtualenvwrapper充满了工具,可以更容易地构建更智能的bash功能版本 - 例如,即使你进入其子目录而不是基地,也可以切换到venv,或者跟踪存储在git或hg中的venv,或者......提示和技巧页面。

The Cookbook for autoenv, shows some nifty ways ways to use the two together.

关于autoenv的Cookbook,展示了一些使用这两者的方法。

#3


0  

Just found in the description of virtualenvwraper this topic

刚刚在virtualenvwraper这个主题的描述中找到了

It describes exactly what I need.

它准确描述了我的需求。