Better way to do this? (bash scripting)

Igor Pechtchanski pechtcha@cs.nyu.edu
Tue Mar 2 17:13:00 GMT 2004


On Tue, 2 Mar 2004, Yaakov Selkowitz wrote:

> I hope this isn't considered too far OT, but perhaps someone will find
> this useful.
>
> I wrote the attached scripts, which I place in /etc/profile.d/, in order
> to get quicker access to the original-package and Cygwin-specific
> documentation.  (pkgdoc and cygdoc respectively)
>
> What I wanted to know is:
>
> 1) is there a better and/or more precise way of searching for the file?

Well, every way I can think of will use 'find' at least once (maybe cache
the results, like 'locate' does).

> 2) is there a better and/or more precise way of verifying that there's
> actually such a file to feed to less, instead of calling find twice?
>
> Thanks,
> Yaakov

a) You can supply multiple directories as starting points to GNU find,
e.g.,

find /usr/bin /usr/include -type f -name \*cygwin\* -print

b) Use the "-r" parameter to xargs, so that the program won't be invoked
unless something is found, e.g.,

find /usr/bin /usr/include -type f -name \*cygwin\* -print0 | xargs -r0 ls -l

c) Use the -path predicate rather than -name.

Also, your scripts are not space-in-filename friendly -- a no-no for
Cygwin -- and the "no documentation" message will be printed for every
directory (which is not quite what you want).  So, with the above fixed,
the first set of 'for' loops in your pkgdoc.sh could become something like

# -------------------
package_dirs=`echo "'${PKGDOC_PATH}'" | sed "s%:%' '%g"`
readme_files=`eval "find $package_dirs -path '*$1-*/$2' -o \
                         -path '*$1/*/$2' -o -path '*$1/$2' -print | \
                    sed -e "s%^%'%" -e "s%$%'%"`
if [ -n "$readme_files" ]; then
  echo $readme_files | xargs -r less
else
  echo "No $2 documentation was found for the $1 package"
fi
STATUS=$? ;;
# -------------------

HTH,
	Igor
-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_		pechtcha@cs.nyu.edu
ZZZzz /,`.-'`'    -.  ;-;;,_		igor@watson.ibm.com
     |,4-  ) )-,_. ,\ (  `'-'		Igor Pechtchanski, Ph.D.
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"I have since come to realize that being between your mentor and his route
to the bathroom is a major career booster."  -- Patrick Naughton

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list