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

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

问题描述

通常在您的 cshrc 文件中有这样的内容来设置路径:

It is typical to have something like this in your cshrc file for setting the path:

set path = ( . $otherpath $path )

但是,当您多次获取 cshrc 文件时,路径会重复,您如何防止重复?

but, the path gets duplicated when you source your cshrc file multiple times, how do you prevent the duplication?

这是一种不干净的做法:

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

推荐答案

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

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

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

$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
"; next; }
    # If the directory is ., set $dot and go around again
    if($_ eq '.') { $dot = 1; next; }

#   if ($_ ne `realpath $_`){
#           printlevel 2, "$_ becomes ".`realpath $_`."
";
#   }
    undef $dest;
    #$_=Stdlib::realpath($_,$dest);
    # Check for duplicates and dot path
    foreach $adir (@newpath) { if($_ eq $adir) { 
        printlevel 2, "Duplicate: $_
";
        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.".
" : "
");

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

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

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

<小时>

我希望那有用.


i hope thats useful.

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

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