资讯专栏INFORMATION COLUMN

Run a Shell Script Auto-matically When Entering/cd

zqhxuyuan / 1713人阅读

I don"t know if it is common that you need to run some shell scripts which are used under only some directories, such as, one of your Rails projects.

Today I find that I always need to run rspec command with a SPEC option, which specifies spec files to be run. In short, everytime I should type the following command in my terminal:

rake spec SPEC=spec/lib/

It is convenient to run this command as an alias, but I don"t want to write this alias into the ~/.bash_profile, because it should be available under the current directory only. But how?

Thanks to the powerful bash shell and its function, we can rewrite the built-in cd command through a function named cd. The following are steps:

    Open your ~/.bash_profile, and insert:

function cd {
    # actually change the directory with all args passed to the function
    builtin cd "$@"
    # if there"s a regular file named ".bash_local"...
    if [ -f ".bash_local" ] ; then
        # source its contents
        source .bash_local
    fi
}

    And then source it in your terminal:

$ source ~/.bash_profile

    Create new file named .bash_local under your target directories(on my machine, it is ~/development/rails-dev/graduation-project/), and then insert:

alias rspec_lib="rake spec SPEC=spec/lib/"

    Now cd the directory, and the alias rspec_lib will be available auto-matically:

$ cd ~/development/rails-dev/graduation-project/
$ rspec_lib
=> .....

Finished in 0.00617 seconds
5 examples, 0 failures

Randomized with seed 23543
Tips

Please consider if it is necessary to check .bash_local into your git repo. If not, remember to add it to the .gitignore file.

TODO

When leave the directory, how to "un-source" the sourced file, that is, make rspec_lib unavailable?

Related Links

    How do you run a shell command/script automatically when entering/cd-ing a directory on Snow Leopard?

    Is it possible to “unsource” in bash?

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/7679.html

相关文章

  • Linux启动分析——init进程与app启动

    摘要:概述本文通过简要分析进程源码,梳理其处理流程,重点关注进程如何启动应用程序,总结启动脚本文件的编写思路进程源码分析进程是内核启动的第一个进程,怎么知道的从内核源码代码的函数分析,可以发现,内核会根据传入的参数来启动第一个进程,一般都是怎么 概述 本文通过简要分析init进程源码,梳理其处理流程,重点关注init进程如何启动应用程序,总结启动脚本文件的编写思路 init进程源码分析 in...

    lidashuang 评论0 收藏0
  • 使用Python和Java调用Shell脚本时的死锁陷阱

    摘要:一般使用或者调用外部脚本需要注意的是,这里的方向是相对于主程序的,所以就是子进程的输出,而是子进程的输入。基于同样的原因,假如调用了方法等待子进程执行完毕而没有及时处理输出的话,就会造成死锁。 最近有一项需求,要定时判断任务执行条件是否满足并触发 Spark 任务,平时编写 Spark 任务时都是封装为一个 Jar 包,然后采用 Shell 脚本形式传入所需参数执行,考虑到本次判断条件...

    freewolf 评论0 收藏0
  • 使用Python和Java调用Shell脚本时的死锁陷阱

    摘要:一般使用或者调用外部脚本需要注意的是,这里的方向是相对于主程序的,所以就是子进程的输出,而是子进程的输入。基于同样的原因,假如调用了方法等待子进程执行完毕而没有及时处理输出的话,就会造成死锁。 最近有一项需求,要定时判断任务执行条件是否满足并触发 Spark 任务,平时编写 Spark 任务时都是封装为一个 Jar 包,然后采用 Shell 脚本形式传入所需参数执行,考虑到本次判断条件...

    wow_worktile 评论0 收藏0
  • Python相对导入导致SystemError的解决方案(译)

    摘要:相对导入相对导入使用模块的决定它是否在一个包内。当你是用类似进行相对导入的时候,点表明在包的层次中上升多少。所以,你不能在交互式会话中直接使用任何相对导入。 源题目与答案地址如下http://stackoverflow.com/questions/14132...。下面是我的翻译(看作机翻也行),以及原文。 这个问题是如何解决在相对导入的时候,如果出现System Error的时候的解...

    ethernet 评论0 收藏0
  • Redis Shell

    摘要:根据带宽,可以计算出最大吞吐量。为了达到高吞吐量突破限制,最后采用的网卡,或者多个网卡。事实上,处理,,的请求时候,吞吐量是差不多的,详细可以见下图。一条经验法则是,的连接数只有连接的一半吞吐量。 Redis提供了redis-cli、redis-server、redis-benchmark等Shell工具。它们虽然比较简单,但是麻雀虽小五脏俱全,有时可以很巧妙地解决一些问题。 redi...

    yeooo 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<