如何避免在 csh 中重复路径变量
通常在您的 cshrc 文件中包含类似的内容来设置路径:
set path = ( . $otherpath $path )
但是,当您多次获取 cshrc 文件时,路径会重复,如何防止重复?
编辑:这是一种不干净的做法:
set localpaths = ( . $otherpaths )
echo ${path} | egrep -i "$localpaths" >& /dev/null
if ($status != 0) then
set path = ( . $otherpaths $path )
endif
It is typical to have something like this in your cshrc file for setting the path:
set path = ( . $otherpath $path )
but, the path gets duplicated when you source your cshrc file multiple times, how do you prevent the duplication?
EDIT: This is one unclean way of doing it:
set localpaths = ( . $otherpaths )
echo ${path} | egrep -i "$localpaths" >& /dev/null
if ($status != 0) then
set path = ( . $otherpaths $path )
endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我很惊讶没有人使用
tr ":" "\n" | grep -x
技术来搜索给定文件夹是否已存在于 $PATH 中。 有什么理由不这样做?在 1 行中:
这是我自己编写的一个函数,可以一次将多个文件夹添加到 $PATH(使用“aaa:bbb:ccc”符号作为参数),在添加之前检查每个文件夹是否有重复项:
可以在如下脚本中调用它this:
它具有以下优点:
!#/bin/sh
完美运行(我用破折号进行了测试)awk
或sed
。希望有帮助!
Im surprised no one used the
tr ":" "\n" | grep -x
techique to search if a given folder already exists in $PATH. Any reason not to?In 1 line:
Here is a function ive made myself to add several folders at once to $PATH (use "aaa:bbb:ccc" notation as argument), checking each one for duplicates before adding:
It can be called in a script like this:
It has the following advantages:
!#/bin/sh
(ive tested with dash)awk
orsed
needed.Hope that helps!
好吧,不是在 csh 中,但这就是我在 bash 中将 $HOME/bin 附加到我的路径中的方式...
季节品尝...
ok, not in csh, but this is how I append $HOME/bin to my path in bash...
season to taste...
您可以使用以下 Perl 脚本来删除重复项的路径。
我希望那有用。
you can use the following Perl script to prune paths of duplicates.
i hope thats useful.
十年来我一直在使用以下 (Bourne/Korn/POSIX/Bash) 脚本:
在 Korn shell 中,我使用:
这使我的 PATH 包含前面的新目录和其他 bin 目录,以及一份主路径值中的每个目录名称,除了旧的和额外的 bin 目录已删除 bin 之外。
您必须将其改编为 C shell(抱歉 - 但我非常相信 C Shell 编程被认为是有害的)。 首先,您不必摆弄冒号分隔符,因此生活实际上更容易。
I've been using the following (Bourne/Korn/POSIX/Bash) script for most of a decade:
In Korn shell, I use:
This leaves me with PATH containing the new and other bin directories at the front, plus one copy of each directory name in the main path value, except that the old and extra bin directories have bin removed.
You would have to adapt this to C shell (sorry - but I'm a great believer in the truths enunciated at C Shell Programming Considered Harmful). Primarily, you won't have to fiddle with the colon separator, so life is actually easier.
好吧,如果您不关心路径的顺序,您可以执行以下操作:
这将对您的路径进行排序并删除任何相同的额外路径。 如果你有 。 在您的路径中,您可能需要使用 grep -v 删除它并在末尾重新添加它。
Well, if you don't care what order your paths are in, you could do something like:
That will sort your paths and remove any extra paths that are the same. If you have . in your path, you may want to remove it with a grep -v and re-add it at the end.
这是一篇没有排序的长单行:
set path = ( echo $path | tr ' ' '\n' | perl -e 'while (<>) { print $_ except $s{$_}++; }' | tr '\ n''')
Here is a long one-liner without sorting:
set path = (
echo $path | tr ' ' '\n' | perl -e 'while (<>) { print $_ unless $s{$_}++; }' | tr '\n' ' '
)dr_peper,
我通常更喜欢坚持使用我所使用的 shell 的脚本功能。使其更加便携。 所以,我喜欢你使用 csh 脚本的解决方案。 我只是将其扩展为在 localdirs 中的每个目录上工作,以使其适合我自己。
dr_peper,
I usually prefer to stick to scripting capabilities of the shell I am living in. Makes it more portable. So, I liked your solution using csh scripting. I just extended it to work on per dir in the localdirs to make it work for myself.
使用 sed(1) 删除重复项。
这将在第一个实例之后删除重复项,这可能是也可能不是您想要的,例如:
享受!
Using sed(1) to remove duplicates.
This will remove the duplicates after the first instance, which may or may not be what you want, e.g.:
Enjoy!
这是我使用的 - 也许其他人会发现它有用:
Here's what I use - perhaps someone else will find it useful:
在 csh 中设置路径(小写,csh 变量)而不是 PATH(环境变量)时,可以使用 set -f 和 set -l,这只会保留每个列表元素的一次出现(首选保留第一个或分别是最后)。
https://nature.berkeley.edu/~casterln/tcsh/Builtin_commands。 html#set
所以像这样
cat foo.csh # 或 .tcshrc 或其他什么:
set -f path = (/bin /usr/bin . ) # 初始值
set -f path = ($path /mycode /hercode /usr/bin ) # 添加新内容和重复内容
每次
source
时,不会继续使用重复项扩展 PATH:% source foo.csh
% 回显 $PATH
% /bin:/usr/bin:.:/mycode:/hercode
% 源文件 foo.csh
% 回显 $PATH
% /bin:/usr/bin:.:/mycode:/hercode
set -f there 确保仅保留每个 PATH 元素的第一次出现。
When setting path (lowercase, the csh variable) rather than PATH (the environment variable) in csh, you can use set -f and set -l, which will only keep one occurrence of each list element (preferring to keep either the first or last, respectively).
https://nature.berkeley.edu/~casterln/tcsh/Builtin_commands.html#set
So something like this
cat foo.csh # or .tcshrc or whatever:
set -f path = (/bin /usr/bin . ) # initial value
set -f path = ($path /mycode /hercode /usr/bin ) # add things, both new and duplicates
Will not keep extending PATH with duplicates every time you
source
it:% source foo.csh
% echo $PATH
% /bin:/usr/bin:.:/mycode:/hercode
% source foo.csh
% echo $PATH
% /bin:/usr/bin:.:/mycode:/hercode
set -f there ensures that only the first occurrence of each PATH element is kept.
我总是在 .cshrc 中从头开始设置路径。
也就是说,我从一条基本路径开始,例如:(
取决于系统)。
然后做:
添加更多东西
I always set my path from scratch in .cshrc.
That is I start off with a basic path, something like:
(depending on the system).
And then do:
to add more stuff
我和原来的问题有同样的需求。
基于您之前的答案,我在 Korn/POSIX/Bash 中使用过:
我很难直接在 csh 中翻译它(csh 转义规则是疯狂的)。 我已经使用过(按照 dr_pepper 的建议):
您有想法进一步简化它(减少管道数量)吗?
I have the same need as the original question.
Building on your previous answers, I have used in Korn/POSIX/Bash:
I had difficulties to translate it directly in csh (csh escape rules are insane). I have used (as suggested by dr_pepper):
Do you have ideas to simplify it more (reduce the number of pipes) ?