简体   繁体   English

我可以让PATH变量求值到我所在的目录吗?

[英]can I have the PATH variable evaluate to what directory I'm in?

On linux using bash , 在使用bash linux
lets say I made two programs both called print_report . 可以说我做了两个程序,都叫做print_report
(They are in different directories.) (它们在不同的目录中。)

Inside my .bashrc file, I have: 在我的.bashrc文件中,我具有:

PATH="path/to/print_report1/:$PATH" PATH =“ path / to / print_report1 /:$ PATH”

This allows me to type print_report anywhere and it will run one of the programs. 这使我可以在任何地方键入print_report ,它将运行其中一个程序。


How can I have bash decide to use one or the other depending on the working directory? 我如何让bash根据工作目录决定使用其中一个?

for example, 例如,
If I'm currently in ~/project1/ and type print_report it will use /bin/foo/print_report 如果我目前在~/project1/然后键入print_report ,它将使用/bin/foo/print_report
If I'm currently in ~/project2/ and type print_report it will use /bin/bar/print_report 如果我目前在~/project2/然后键入print_report ,它将使用/bin/bar/print_report

You can't do that as such. 您不能那样做。 Instead, write a wrapper script or function that checks the current directory and invokes the right command: 而是编写一个检查当前目录并调用正确命令的包装器脚本或函数:

#!/bin/bash
if [[ $PWD == $HOME/project1/* ]]
then
    /bin/foo/print_report "$@"
elif [[ $PWD == $HOME/project2/* ]] 
then
    /bin/bar/print_report "$@"
else
    echo "Don't know how to print_report for $PWD"
fi

This is a security disaster waiting to happen, (which is to say you really don't want to do this) but you can certainly do something like: 这是一场等待发生的安全灾难(也就是说,您确实不想这样做),但是您可以执行以下操作:

cd() {
    dir=${1-.}
    case $dir in)
        path1) PATH=/path/for/working/in/path1;;
        path2) PATH=/path/for/working/in/path2;;
        *) PATH=/bin:/usr/bin;;
    esac
    command cd $dir
}

(Put that in your .bashrc or just define it in the current shell.) (将其放在您的.bashrc中或仅在当前shell中对其进行定义。)

You can emulate preexec hooks à la zsh, using the DEBUG trap. 您可以使用DEBUG陷阱模拟zz的preexec挂钩

In that way, every time a command is executed, you can run a preexec hook to check $PWD, and adjust $PATH accordingly. 这样,每次执行命令时,您都可以运行preexec挂钩来检查$ PWD,并相应地调整$ PATH。

You can include a preexec hook doing what you want in your .bashrc. 您可以在.bashrc中包含一个preexec挂钩,以执行所需的操作。

Everything presented here so far strikes me as overly complicated and needlessly complex hackery. 到目前为止,这里介绍的所有内容都使我感到过于复杂和不必要地复杂。 I would just place a Makefile in each directory with 我只是将Makefile放在每个目录中

report:
        /bin/foo/print_report

in ~/project1/Makefile , and ~/project1/Makefile ,和

report:
        /bin/bar/print_report

in ~/project2/Makefile . ~/project2/Makefile This extends easily to as many directories and programs you want. 这很容易扩展到所需的目录和程序。 And you only need to type make instead of those longwinded command names :-) 而且您只需要键入make而不是那些冗长的命令名称即可:-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为目录创建别名,这样就不必每次都键入长路径? - How can I create an alias to a directory so that I don't have to type the long path every time? 我可以使用shell别名来评估历史替换命令吗? - Can I have a shell alias evaluate a history substitution command? 如何在$ PATH变量中添加目录,以便可以在bash中运行脚本而无需输入整个文件路径? - How can I add a directory to my $PATH variable so that I may run scripts in bash without typing out the entire file path? 我可以将xargs用作目录路径的一部分吗? - Can I use xargs as part of a directory path? 如何根据我工作的目录更改PS1? - how to change PS1 based on what directory i`m working in? 如何从$ PATH中删除条目? 我尝试使用nano打开$ HOME / .bashrc,但似乎没有我要找的东西 - How do I remove entries from $PATH? I tried using nano to open $HOME/.bashrc but it doesn't seem to have what I'm looking for Bash - 检查我是否对任何目录路径的父级有写权限 - Bash - check if I have write permissions to parent of the any directory path 如何遍历存储在变量中的目录路径 - How do I loop through a directory path stored in a variable 我正在尝试创建一个自定义 $PS1 来显示我所在的每个目录中有多少文件并打印结果 - I'm trying to create a custom $PS1 that shows how many files I have in each directory I'm in and print the results 如何从 bash 的 PATH 环境变量中获取第一个目录? - How to I get the first directory from the PATH environment variable in bash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM