如何避免在csh中重复路径变量 [英] How to keep from duplicating path variable in csh

查看:232
本文介绍了如何避免在csh中重复路径变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 设置路径=(。$ otherpath $ path)

但是,当您多次使用cshrc文件时,路径会重复,如何你阻止重复?



编辑:这是一个不干净的方法:

 code> set localpaths =(。$ otherpaths)
echo $ {path} | egrep -i$ localpaths>& / dev / null
if($ status!= 0)then
set path =(。$ otherpaths $ path)
endif


解决方案

您可以使用以下Perl脚本修剪重复的路径。






 #!/ usr / bin / perl 

#^^确保这是指向正确的位置。

#标题:SLimPath
#作者:DavidShoe LacePyke< eselle@users.sourceforge.net>
#:Tim Nelson
#目的:创建一个我的环境路径的苗条版本,以消除
#重复条目,并确保。路径是最后的。
#创建日期:1999年4月1日
#修订历史:
#01/04/99:初始测试..没有wok verywell
#:retreived path throught '$ ENV'调用
#07/04/99:在Tim Nelson< wayland@ne.com.au>得到它
#工作。
#:used'push'添加到数组
#:used'join'从列表/数组创建一个分隔的字符串。
#16/02/00:修复cmd行选项查看/更好的工作
#25/02/00:make verbosity level-oriented



使用Getopt :: Std;

sub printlevel;

$ initial_str =;
$ debug_mode =;
$ delim_chr =:;
$ opt_v = 1;

getopts(v:hd:l:e:s:);

OPTS:{
$ opt_h&&& do {
print\\\
$ 0 [-v level] [-d level] [-l delim](-e varname | -s strname | -h);
打印\\\
Where:;
打印\\\
-h此帮助;
打印\\\
-d调试级别;
print\\\
-l Delimiter(between path vars);
print\\\
-e指定环境变量(NB:不包括\ $ sign);
print\\\
-s String(即$ 0 -s \ $ PATH:/ looser / bin /);
print\\\
-v Verbosity(0 = quiet,1 = normal,2 = verbose);
打印\\\
;
退出;
};
$ opt_d&&& do {
printlevel 1,你选择了调试级别$ opt_d\\\
;
$ debug_mode = $ opt_d;
};
$ opt_l&&& do {
printlevel 1,你要用\$ opt_l\\\\
分隔字符串;
$ delim_chr = $ opt_l;
};
$ opt_e&&& do {
if($ opt_s){dieCan not specified BOTH env var and string\\\
; }
printlevel 1,使用环境变量\$ opt_e\\\\
;
$ initial_str = $ ENV {$ opt_e};
};
$ opt_s&&& do {
printlevel 1,Using String \$ opt_s\\\\
;
$ initial_str = $ opt_s;
};
}

如果(($#ARGV!= 1)和!$ opt_e和!$ opt_s){
die无法使用 - 尝试$ 0 -h \
;
}

$ what = shift @ARGV;
#使用分隔符分割路径
@dirs = split(/ $ delim_chr /,$ initial_str);

$ dest;
@newpath =();
LOOP:foreach(@dirs){
#确保目录存在,是一个目录
if(!-e){printlevel 1,$ _ not exist\\\
;下一个; }
#如果目录是。,设置$ dot并再次绕行
if($ _ eq'。'){$ dot = 1;下一个; }

#if($ _ ne`realpath $ _`){
#printlevel 2,$ _ become.`realpath $ _`。\\\
;
#}
undef $ dest;
#$ _ = Stdlib :: realpath($ _,$ dest);
#检查重复项和点路径
foreach $ adir(@newpath){if($ _ eq $ adir){
printlevel 2,Duplicate:$ _\\\
;
下一个LOOP;
}}

push @newpath,$ _;
}

#Join从由第一个表达式
print join($ delim_chr,@newpath)分隔的列表/数组中创建一个字符串。 ($ dot?$ delim_chr。。\\\
:\\\
);

printlevel 1,谢谢你使用$ 0\\\
;
退出;

sub printlevel {
my($ level,$ string)= @_;

if($ opt_v> = $ level){
print STDERR $ string;
}
}






我希望这有用。


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

解决方案

you can use the following Perl script to prune paths of duplicates.


#!/usr/bin/perl
#
# ^^ ensure this is pointing to the correct location.
#
# Title:    SLimPath
# Author:   David "Shoe Lace" Pyke <eselle@users.sourceforge.net >
#   :   Tim Nelson 
# Purpose: To create a slim version of my envirnoment path so as to eliminate
#       duplicate entries and ensure that the "." path was last.
# Date Created: April 1st 1999
# Revision History:
#   01/04/99: initial tests.. didn't wok verywell at all
#       : retreived path throught '$ENV' call
#   07/04/99: After an email from Tim Nelson <wayland@ne.com.au> got it to
#         work.
#       : used 'push' to add to array
#       : used 'join' to create a delimited string from a list/array.
#   16/02/00: fixed cmd-line options to look/work better
#   25/02/00: made verbosity level-oriented
#
#

use Getopt::Std;

sub printlevel;

$initial_str = "";
$debug_mode = "";
$delim_chr = ":";
$opt_v = 1;

getopts("v:hd:l:e:s:");

OPTS: {
    $opt_h && do {
print "\n$0 [-v level] [-d level] [-l delim] ( -e varname | -s strname | -h )";
print "\nWhere:";
print "\n   -h  This help";
print "\n   -d  Debug level";
print "\n   -l  Delimiter (between path vars)";
print "\n   -e  Specify environment variable (NB: don't include \$ sign)";
print "\n   -s  String (ie. $0 -s \$PATH:/looser/bin/)";
print "\n   -v  Verbosity (0 = quiet, 1 = normal, 2 = verbose)";
print "\n";
        exit;
    };
    $opt_d && do {
        printlevel 1, "You selected debug level $opt_d\n";
        $debug_mode = $opt_d;
    };
    $opt_l && do {
        printlevel 1, "You are going to delimit the string with \"$opt_l\"\n";
        $delim_chr = $opt_l;
    };
    $opt_e && do {
        if($opt_s) { die "Cannot specify BOTH env var and string\n"; }
        printlevel 1, "Using Environment variable \"$opt_e\"\n";
        $initial_str = $ENV{$opt_e};
    };
    $opt_s && do {
        printlevel 1, "Using String \"$opt_s\"\n";
        $initial_str = $opt_s;
    };
}

if( ($#ARGV != 1) and !$opt_e and !$opt_s){
    die "Nothing to work with -- try $0 -h\n";
}

$what = shift @ARGV;
# Split path using the delimiter
@dirs = split(/$delim_chr/, $initial_str);

$dest;
@newpath = ();
LOOP: foreach (@dirs){
    # Ensure the directory exists and is a directory
    if(! -e ) { printlevel 1, "$_ does not exist\n"; next; }
    # If the directory is ., set $dot and go around again
    if($_ eq '.') { $dot = 1; next; }

#   if ($_ ne `realpath $_`){
#           printlevel 2, "$_ becomes ".`realpath $_`."\n";
#   }
    undef $dest;
    #$_=Stdlib::realpath($_,$dest);
    # Check for duplicates and dot path
    foreach $adir (@newpath) { if($_ eq $adir) { 
        printlevel 2, "Duplicate: $_\n";
        next LOOP; 
    }}

    push @newpath, $_;
}

# Join creates a string from a list/array delimited by the first expression
print join($delim_chr, @newpath) . ($dot ? $delim_chr.".\n" : "\n");

printlevel 1, "Thank you for using $0\n";
exit;

sub printlevel {
    my($level, $string) = @_;

    if($opt_v >= $level) {
        print STDERR $string;
    }
}


i hope thats useful.

这篇关于如何避免在csh中重复路径变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆