zsh-users
 help / color / mirror / code / Atom feed
* fewer forks
@ 2009-05-25 13:59 Scott Lipcon
  2009-05-25 16:26 ` Andrey Borzenkov
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Scott Lipcon @ 2009-05-25 13:59 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1238 bytes --]

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.

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.

Thanks,
Scott

[-- Attachment #2: Type: text/html, Size: 1385 bytes --]

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

* Re: fewer forks
  2009-05-25 13:59 fewer forks Scott Lipcon
@ 2009-05-25 16:26 ` Andrey Borzenkov
  2009-05-25 16:56 ` Daniel Friesel
  2009-05-25 17:05 ` Bart Schaefer
  2 siblings, 0 replies; 4+ messages in thread
From: Andrey Borzenkov @ 2009-05-25 16:26 UTC (permalink / raw)
  To: zsh-users; +Cc: Scott Lipcon

[-- Attachment #1: Type: Text/Plain, Size: 1633 bytes --]

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 #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

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

* Re: fewer forks
  2009-05-25 13:59 fewer forks Scott Lipcon
  2009-05-25 16:26 ` Andrey Borzenkov
@ 2009-05-25 16:56 ` Daniel Friesel
  2009-05-25 17:05 ` Bart Schaefer
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Friesel @ 2009-05-25 16:56 UTC (permalink / raw)
  To: Scott Lipcon, zsh-users

On Mon, May 25, 2009 at 09:59:56AM -0400, Scott Lipcon wrote:
> 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.

You could iterate over ${(k)parameters} and check for [[ $dir == *ROOT &&
$dir != CVSROOT ]].

> 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.
> 

grep should be the best choice there. You can of course use read
instead and then check each line, but I guess that would be a lot
slower.
Same goes for grep | sed | tr - When it comes to operating on streams,
I found these to be far more efficient than the zsh builtins (which
usually operate on single values and need to be wrapped into read or
similar).


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

* Re: fewer forks
  2009-05-25 13:59 fewer forks Scott Lipcon
  2009-05-25 16:26 ` Andrey Borzenkov
  2009-05-25 16:56 ` Daniel Friesel
@ 2009-05-25 17:05 ` Bart Schaefer
  2 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2009-05-25 17:05 UTC (permalink / raw)
  To: zsh-users

On May 25,  9:59am, Scott Lipcon wrote:
}
} 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.
} 
} 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

  function setLDPath {
    zmodload zsh/parameter
    ld_library_path=()
    for dir in ${(k)parameters[(I)*ROOT]}; do
      if [[ $dir != CVSROOT && $parameters[$dir] = *export* ]]; then
        ld_library_path+=( ${(P)dir}/lib )
      fi
    done
    ld_library_path+=( $base_ld_path )
  }

That's not quite identical because it builds by appending in arbitrary
order rather than prepending in alphabetical order; if for some reason
alphabetical order actually matters, use ${(Ok)parameters[(I)*ROOT]}.

} 2) grep for a pattern in a file.

In general zsh is not going to do this as well as grep and thus saving
the overhead of the fork worth it.  In your specific example, though:

} The project i work on has a csh script to set some variables
} (specifically, the $FOOROOT variables, above.

It may very well be possible to define setenv and possibly a few other
zsh functions and then simply "source" the csh script.  For example I
use:

  setenv () {
    typeset -x "${1}${1:+=}${(@)argv[2,$#]}"
  }

} then I have another grep piped to sed and tr to get (and slightly transform)
} the value.

Aside: It's almost never necessary to pipe grep to sed, because sed can
do the pattern search itself.

Depending on what you're doing with sed and tr, you can probably apply
the transformation to ${(@)argv[2,$#]} inside the setenv function using
zsh parameter manipulations.


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

end of thread, other threads:[~2009-05-25 17:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-25 13:59 fewer forks Scott Lipcon
2009-05-25 16:26 ` Andrey Borzenkov
2009-05-25 16:56 ` Daniel Friesel
2009-05-25 17:05 ` Bart Schaefer

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).