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

Re: fewer forks



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.



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