Jak nie powielać zmiennej path w csh

Typowe jest posiadanie czegoś takiego w pliku cshrc do ustawiania ścieżki:

set path = ( . $otherpath $path )

Ale, ścieżka jest duplikowana, gdy źródłowy plik cshrc wiele razy, jak zapobiec duplikacji?

EDIT: to jest jeden nieczysty sposób na zrobienie tego:

set localpaths = ( . $otherpaths )
echo ${path} | egrep -i "$localpaths" >& /dev/null
if ($status != 0) then
    set path = ( . $otherpaths $path )
endif
Author: LPL, 2008-09-26

11 answers

Możesz użyć następującego skryptu Perla do przycinania ścieżek duplikatów.


#!/usr/bin/perl
#
# ^^ ensure this is pointing to the correct location.
#
# Title:    SLimPath
# Author:   David "Shoe Lace" Pyke <[email protected] >
#   :   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 <[email protected]> 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;
    }
}

Mam nadzieję, że to się przyda.
 3
Author: ShoeLace,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-26 13:11:41

Jestem zaskoczony, że nikt nie użył tr ":" "\n" | grep -x techique do wyszukiwania, jeśli dany folder już istnieje w $PATH. Jakiś powód, żeby tego nie robić?

W 1 linii:

if ! $(echo "$PATH" | tr ":" "\n" | grep -qx "$dir") ; then PATH=$PATH:$dir ; fi

Oto funkcja, którą zrobiłem, aby dodać kilka folderów na raz do $PATH (użyj notacji "aaa: BBB: ccc"jako argumentu), sprawdzając każdy z nich pod kątem duplikatów przed dodaniem:

append_path()
{
    local SAVED_IFS="$IFS"
    local dir
    IFS=:
    for dir in $1 ; do
        if ! $( echo "$PATH" | tr ":" "\n" | grep -qx "$dir" ) ; then
            PATH=$PATH:$dir
        fi
    done
    IFS="$SAVED_IFS"
}

Można go wywołać w skrypcie takim jak:

append_path "/test:$HOME/bin:/example/my dir/space is not an issue"

Ma następujące zalety:

  • żadnych bashismów ani żadnej składni specyficznej dla powłoki. To działa idealnie z !#/bin/sh (Ive testowane z dash)
  • można dodawać wiele folderów jednocześnie
  • brak sortowania, zachowuje kolejność katalogów
  • doskonale radzi sobie ze spacjami w nazwach katalogów
  • pojedynczy test działa bez względu na to, czy $folder znajduje się na początku, końcu, środku, czy jest jedynym folderem w $PATH (unikając w ten sposób testowania X:*, *:x, :x:, x, jak wiele rozwiązań tutaj domyślnie robi)
  • działa (i zachowuje), jeśli $PATH zaczyna się lub kończy znakiem": "lub ma":: "w it (czyli bieżący katalog)
  • nie awk lub sed potrzebne.
  • EPA friendly ;) oryginalna wartość IFS jest zachowana, a wszystkie inne zmienne są lokalne do zakresu funkcji.
Mam nadzieję, że to pomoże!
 13
Author: MestreLion,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-29 04:57:26

Ok, nie w csh, ale w ten sposób dodaję $HOME/bin do mojej ścieżki w bash...

case $PATH in
    *:$HOME/bin | *:$HOME/bin:* ) ;;
    *) export PATH=$PATH:$HOME/bin
esac
Doprawić do smaku...
 4
Author: mike511,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-25 21:07:57

Przez większość dekady używałem następującego skryptu (Bourne/Korn/POSIX/Bash):

:   "@(#)$Id: clnpath.sh,v 1.6 1999/06/08 23:34:07 jleffler Exp $"
#
#   Print minimal version of $PATH, possibly removing some items

case $# in
0)  chop=""; path=${PATH:?};;
1)  chop=""; path=$1;;
2)  chop=$2; path=$1;;
*)  echo "Usage: `basename $0 .sh` [$PATH [remove:list]]" >&2
    exit 1;;
esac

# Beware of the quotes in the assignment to chop!
echo "$path" |
${AWK:-awk} -F: '#
BEGIN   {   # Sort out which path components to omit
            chop="'"$chop"'";
            if (chop != "") nr = split(chop, remove); else nr = 0;
            for (i = 1; i <= nr; i++)
                omit[remove[i]] = 1;
        }
{
    for (i = 1; i <= NF; i++)
    {
        x=$i;
        if (x == "") x = ".";
        if (omit[x] == 0 && path[x]++ == 0)
        {
            output = output pad x;
            pad = ":";
        }
    }
    print output;
}'

W powłoce Korn używam:

export PATH=$(clnpath /new/bin:/other/bin:$PATH /old/bin:/extra/bin)

To pozostawia mi ścieżkę zawierającą nowe i inne katalogi bin z przodu, plus jedną kopię każdej nazwy katalogu w głównej wartości ścieżki, z wyjątkiem tego, że stare i dodatkowe katalogi bin zostały usunięte.

Trzeba by dostosować to do C shell (sorry - ale jestem wielkim zwolennikiem prawd wypowiedzianych w C Shell Programming Uważany Za Szkodliwy ). Przede wszystkim nie będziesz musiał bawić się separatorem jelita grubego, więc życie jest łatwiejsze.

 2
Author: Jonathan Leffler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-26 06:03:54

Cóż, jeśli nie obchodzi cię, w jakiej kolejności są twoje ścieżki, możesz zrobić coś takiego:

set path=(`echo $path | tr ' ' '\n' | sort | uniq | tr '\n' ' '`)

To posortuje twoje ścieżki i usunie wszystkie dodatkowe ścieżki, które są takie same. Jeśli tak . w ścieżce możesz usunąć go za pomocą grep-v i ponownie dodać go na końcu.

 2
Author: Steve Baker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-29 13:46:03

Oto długa jednowierszowa bez sortowania:
Ustaw ścieżkę = ( echo $path | tr ' ' '\n' | perl -e 'while (<>) { print $_ unless $s{$_}++; }' | tr '\n' ' ')

 2
Author: dr_pepper,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-30 20:38:55

Dr_peper,

Zazwyczaj wolę trzymać się możliwości skryptowych powłoki, w której żyję. Sprawia, że jest bardziej przenośny. Spodobało mi się Twoje rozwiązanie wykorzystujące Skrypty csh. Po prostu rozszerzyłem go do pracy na PER dir w lokalnych, aby działał dla siebie.

foreach dir ( $localdirs )
    echo ${path} | egrep -i "$dir" >& /dev/null
    if ($status != 0) then
        set path = ( $dir $path )
    endif
end
 2
Author: Madhur Kashyap,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-19 05:26:37

Używanie sed(1) do usuwania duplikatów.

$ PATH=$(echo $PATH | sed -e 's/$/:/;s/^/:/;s/:/::/g;:a;s#\(:[^:]\{1,\}:\)\(.*\)\1#\1\2#g;ta;s/::*/:/g;s/^://;s/:$//;')

Spowoduje to usunięcie duplikatów po pierwszej instancji, które mogą być, ale nie muszą być tym, czego chcesz, np.:

$ NEWPATH=/bin:/usr/bin:/bin:/usr/local/bin:/usr/local/bin:/bin
$ echo $NEWPATH | sed -e 's/$/:/; s/^/:/; s/:/::/g; :a; s#\(:[^:]\{1,\}:\)\(.*\)\1#\1\2#g; t a; s/::*/:/g; s/^://; s/:$//;'
/bin:/usr/bin:/usr/local/bin
$
Smacznego!
 2
Author: Brad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-28 09:36:53

Oto czego używam - może komuś innemu się przyda:

#!/bin/csh
#  ABSTRACT
#    /bin/csh function-like aliases for manipulating environment
#    variables containing paths.
#
#  BUGS
#    - These *MUST* be single line aliases to avoid parsing problems apparently related
#      to if-then-else
#    - Aliases currently perform tests in inefficient in order to avoid parsing problems
#    - Extremely fragile - use bash instead!!
#
#  AUTHOR
#    J. P. Abelanet - 11/11/10

#  Function-like alias to add a path to the front of an environment variable
#    containing colon (':') delimited paths, without path duplication
#
#  Usage: prepend_path ENVVARIABLE /path/to/prepend
alias prepend_path \
  'set arg2="\!:2";  if ($?\!:1 == 0) setenv \!:1 "$arg2";  if ($?\!:1 && $\!:1 !~ {,*:}"$arg2"{:*,}) setenv \!:1 "$arg2":"$\!:1";'

#  Function-like alias to add a path to the back of any environment variable 
#    containing colon (':') delimited paths, without path duplication
#
#  Usage: append_path ENVVARIABLE /path/to/append
alias append_path \
  'set arg2="\!:2";  if ($?\!:1 == 0) setenv \!:1 "$arg2";  if ($?\!:1 && $\!:1 !~ {,*:}"$arg2"{:*,}) setenv \!:1 "$\!:1":"$arg2";'
 1
Author: jpabel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-04 22:02:17

Zawsze ustawiam swoją ścieżkę od zera .cshrc. To znaczy, że zaczynam od podstawowej ścieżki, coś w stylu:

set path = (. ~/bin /bin /usr/bin /usr/ucb /usr/bin/X11)

(w zależności od systemu).

A potem zrobić:

set path = ($otherPath $path)

Aby dodać więcej rzeczy

 0
Author: Andrew Stein,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-25 20:33:10

Mam taką samą potrzebę jak pierwotne pytanie. Bazując na twoich poprzednich odpowiedziach, użyłem w Korn / POSIX / Bash:

export PATH=$(perl -e 'print join ":", grep {!$h{$_}++} split ":", "'$otherpath:$PATH\")

Miałem trudności, aby przetłumaczyć go bezpośrednio w csh (csh escape rules are insane). Użyłem (zgodnie z sugestią dr_peppera):

set path = ( `echo $otherpath $path | tr ' ' '\n' | perl -ne 'print $_ unless $h{$_}++' | tr '\n' ' '`)

Masz pomysł, aby to bardziej uprościć (zmniejszyć liczbę rur)?

 0
Author: BOC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-18 13:04:23