zsh-users
 help / color / mirror / code / Atom feed
* PATH editing in a script
@ 1998-02-24 13:11 Helmut Jarausch
  1998-02-24 13:31 ` Bruce Stephens
  1998-02-24 14:28 ` Bernd Eggink
  0 siblings, 2 replies; 6+ messages in thread
From: Helmut Jarausch @ 1998-02-24 13:11 UTC (permalink / raw)
  To: zsh-users

Hi,
does anybody know how to edit the PATH environment variable?
Something like 'vared' but in a script (like sed)

Problem:

Having two versions of (say) TeX I would like to replace the PATH to tex
in a user PATH environment string.

Like
s|/usr/local/lib/TeX|/usr/local/teTeX|

It's no problem to edit the PATH e.g. by Perl but these changes have to
persist whence the (Perl/Zsh) script finishes.

Thanks for any hints,
Helmut.







^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: PATH editing in a script
  1998-02-24 13:11 PATH editing in a script Helmut Jarausch
@ 1998-02-24 13:31 ` Bruce Stephens
  1998-02-24 14:28 ` Bernd Eggink
  1 sibling, 0 replies; 6+ messages in thread
From: Bruce Stephens @ 1998-02-24 13:31 UTC (permalink / raw)
  To: zsh-users

jarausch@IGPM.Rwth-Aachen.DE said:
> Having two versions of (say) TeX I would like to replace the PATH to
> tex in a user PATH environment string.

> Like s|/usr/local/lib/TeX|/usr/local/teTeX|

> It's no problem to edit the PATH e.g. by Perl but these changes have
> to persist whence the (Perl/Zsh) script finishes. 

You can't.  Environment variables are local to processes (and may get 
inherited by child processes, but that doesn't help here).

You can do something like (warning: untested code ahead):

PATH=$(echo $PATH | sed 's|/usr/local/lib/TeX|/usr/local/teTeX|')

You could stick this in a shell function (possibly autoloaded) or an alias.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: PATH editing in a script
  1998-02-24 13:11 PATH editing in a script Helmut Jarausch
  1998-02-24 13:31 ` Bruce Stephens
@ 1998-02-24 14:28 ` Bernd Eggink
  1998-03-02  1:06   ` Hank Hughes
  1 sibling, 1 reply; 6+ messages in thread
From: Bernd Eggink @ 1998-02-24 14:28 UTC (permalink / raw)
  To: Helmut Jarausch; +Cc: zsh-users

Helmut Jarausch wrote:
> 
> Hi,
> does anybody know how to edit the PATH environment variable?
> Something like 'vared' but in a script (like sed)
> 
> Problem:
> 
> Having two versions of (say) TeX I would like to replace the PATH to tex
> in a user PATH environment string.
> 
> Like
> s|/usr/local/lib/TeX|/usr/local/teTeX|
> 
> It's no problem to edit the PATH e.g. by Perl but these changes have to
> persist whence the (Perl/Zsh) script finishes.

For the changes to be persistent, you have to execute the script with
the "." special command. A simple solution:

  path=($new ${(R)path:#$old})

where $old is the component you want to remove and $new the one you want
to add. Another solution, which keeps the original order:

  path[$path[(ri)$old]]=$new 

Hope that helps,
Bernd

-- 
Bernd Eggink
Regionales Rechenzentrum der Universitaet Hamburg
eggink@rrz.uni-hamburg.de
http://www.rrz.uni-hamburg.de/eggink/BEggink.html


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: PATH editing in a script
  1998-02-24 14:28 ` Bernd Eggink
@ 1998-03-02  1:06   ` Hank Hughes
  1998-03-03 10:31     ` Bernd Eggink
  0 siblings, 1 reply; 6+ messages in thread
From: Hank Hughes @ 1998-03-02  1:06 UTC (permalink / raw)
  To: Bernd Eggink; +Cc: Helmut Jarausch, zsh-users


I made some `quick' hacks to edit my path whilst in hustle-mode.
The functions are listed below. Are there more correct or efficient 
ways to do any of these?

----------------------------------------------------------------------
#
# PATH adjustors
#
# Concepts originally from (Well, at least the first time *I* saw them)
#    Remy Evard <evard@mcs.anl.gov>
#
# Re-Inspired by parameter expansions given by 
#    Bernd Eggink <eggink@rrz.uni-hamburg.de>
#

paths()                              # Print the index order and the directory
{ 
    # TODO: find a builtin sequence to replace awk hell
    #       something like `foreach dir ( $path ) { printf() }'?

    usage="Usage: (-i|-n) or (-d|-a) for numeric or alphabetical order."

    if [ $1 ] 
    then 
	if ( [ $1 = "-i" ] || [ $1 = "-n" ] ) 
	then                         # Sort numerically by index
	    echo "Index   Directory"
	    echo "-----   ------------------------------------------------------------------------"
	    echo $PATH | awk -F: '{ for (i = 1; i <= NF; i++) printf("  %3d    %s\n", ++num, $i)}' | sort
	elif ( [ $1 = "-d" ] || [ $1 = "-a" ] )
        then                         # Sort alphabetically by directory
	    echo "Index   Directory"
	    echo "-----   ------------------------------------------------------------------------"
	    echo $PATH | awk -F: '{ for (i = 1; i <= NF; i++) printf("  %3d    %s\n", ++num, $i)}' | sort +1
	else
	    echo $usage              # Maybe default by index instead?
	fi
    else
        echo $usage
    fi
}

addpath()   { path=($path:$1) }      # Append to path
prepath()   { path=($1:$path) }      # Prepend to path
rmpath()                             # Remove from path
{                                       # Depending on tab complete ...
    dir=`echo $1 | sed 's/\/$//'`       #   Remove finicky trailing slash 
    path=(${(R)path:#$dir})             # Param expansion ...
				        #   ${name:#patt} replace with null
}
replpath()                           # Replace path with path
{ 
    dir1=`echo $1 | sed 's/\/$//'`      # The trailing slashes ..
    dir2=`echo $2 | sed 's/\/$//'`
    path[$path[(ri)$dir1]]=$dir2        # Replace matched index with new directory
} 

#nognu()   { rmpath /arch/gnu/bin }     # Zzzot gnu!



----------------------------------------------------------------------


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: PATH editing in a script
  1998-03-02  1:06   ` Hank Hughes
@ 1998-03-03 10:31     ` Bernd Eggink
  0 siblings, 0 replies; 6+ messages in thread
From: Bernd Eggink @ 1998-03-03 10:31 UTC (permalink / raw)
  To: Helmut Jarausch; +Cc: zsh-users

Hank Hughes wrote:
> 
> I made some `quick' hacks to edit my path whilst in hustle-mode.
> The functions are listed below. Are there more correct or efficient
> ways to do any of these?

Hm, how about this (caution, some long lines may be broken up by the
mailer):

--------<snip> --------

paths()                              # Print the index order and the
directory
{ 
    # TODO: find a builtin sequence to replace awk hell
    #       something like `foreach dir ( $path ) { printf() }'?

    usage="Usage: (-i|-n) or (-d|-a) for numeric or alphabetical order."

    typeset     p psort par
    typeset -R5 i=1

    while getopts "inda" par
    do
        case $par in
        (i|n)
            print "Index   Directory"
            print - "----- 
------------------------------------------------------------------------"
            for p in $path
            do
                print "$i   $p"
                (( ++i ))
            done
            ;;
        (d|a)
            print "Index   Directory"
            print - "-----   
------------------------------------------------------------------------"

            for p in ${(o)path}
            do
                i=$path[(ri)$p]
                print "$i   $p"
            done
            ;;
        (*)
            print "$usage" 
        esac
    done
}

addpath()   { path=($path $1) }      # Append to path
prepath()   { path=($1 $path) }      # Prepend to path
rmpath()                             # Remove from path
{                                    # Depending on tab complete ...
    path=(${(R)path:#${1%/}})  
}
replpath()                           # Replace path with path
{ 
    path[$path[(ri)${1%/}]]=${2%/}   # Replace matched index with new
directory
} 

--------<snap> --------

You don't need 'awk'. Zsh can do it all. Note that the way you added a
component to
$path has been wrong. $path is an array, $PATH isn't, so either
path=($path $1)
or PATH=$PATH:$1, but not path=($path:$1)

BTW, for practical use you'd probabely better use an index as parameter
for
rmpath and replpath.

Regards,
	Bernd

--
Bernd Eggink
Regionales Rechenzentrum der Universitaet Hamburg
eggink@rrz.uni-hamburg.de
http://www.rrz.uni-hamburg.de/eggink/BEggink.html


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: PATH editing in a script
@ 1998-02-24 14:55 Niall Smart
  0 siblings, 0 replies; 6+ messages in thread
From: Niall Smart @ 1998-02-24 14:55 UTC (permalink / raw)
  To: Helmut Jarausch; +Cc: zsh-users

On Feb 24,  2:11pm, Helmut Jarausch wrote:
} Subject: PATH editing in a script
> Hi,
> does anybody know how to edit the PATH environment variable?
> Something like 'vared' but in a script (like sed)
> 
> Problem:
> 
> Having two versions of (say) TeX I would like to replace the PATH to tex
> in a user PATH environment string.
> 
> Like
> s|/usr/local/lib/TeX|/usr/local/teTeX|
> 
> It's no problem to edit the PATH e.g. by Perl but these changes have to
> persist whence the (Perl/Zsh) script finishes.

As someone has already pointed out, the environment list resides in a
processes address space and you can't easily go poking around in another
processes address space to change it.

One technique I've seen used is:

alias foo="eval `foo.real`"

foo.real:

#!/bin/sh
echo export PATH=$PATH | sed 's,/usr/local/lib/TeX,/usr/local/teTeX,g'

Niall


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1998-03-03 11:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-02-24 13:11 PATH editing in a script Helmut Jarausch
1998-02-24 13:31 ` Bruce Stephens
1998-02-24 14:28 ` Bernd Eggink
1998-03-02  1:06   ` Hank Hughes
1998-03-03 10:31     ` Bernd Eggink
1998-02-24 14:55 Niall Smart

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).