Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: fewer forks



On Monday 25 of May 2009 17:59:56 Scott Lipcon wrote:
> I have a couple of shell functions that I use to manipulate my
> environment and I'm looking to see if these can be done in a more
> "zsh way", specifically with fewer forks to external programs.
>
> 1)  look for environment variables matching a pattern.   I have a
> number of environment variables that all end in ROOT,and I need to
> add each $FOOROOT/bin to my path, $FOOROOT/lib to my LD_LIBRARY_PATH,
> etc. Currently I have functions like:
>
>
> function setLDPath {
>     ld_library_path=( $base_ld_path )
>
>     for dir in `=env | =grep ROOT | =grep -v 'CVSROOT' | =cut -f1
> -d=`; do ld_library_path=( $(eval "echo \$$dir/lib") $ld_library_path
> ) done
> }
>
> but thats a lot of external processes (env, grep, grep, cut, echo) to
> do something that seems like it should be simple.
>

for dir in ${${(M)${(k)parameters[(R)*export*]}:#*ROOT}:#CVSROOT}; do
	ld_library_path=( ${(P)dir}/lib $ld_library_path )
done

> 2) grep for a pattern in a file.  The project i work on has a csh
> script to set some variables (specifically, the $FOOROOT variables,
> above.  Currently I'm doing:
>
>             for root in `=grep '^setenv .*ROOT ' $REQS | =cut -f2 -d'
> '`; do ...
>             done
>
> which pulls the name of the environment variable out of the file
> ($REQS) and then I have another grep piped to sed and tr to get (and
> slightly transform) the value.
>

print -l ${${${(M)${(f)"$(</tmp/foo)"}:#setenv *ROOT *}/#setenv }% *}

assuming /tmp/foo contains the script. You can tailor patterns to suite 
your needs.

Attachment: signature.asc
Description: This is a digitally signed message part.



Messages sorted by: Reverse Date, Date, Thread, Author