开发者

sh - Build string containing paths

开发者 https://www.devze.com 2022-12-12 02:27 出处:网络
I\'m building a small shell script for finding cat/man pages on a wide range of unix systems... in bash, I could build all possible paths开发者_开发技巧 by doing this:

I'm building a small shell script for finding cat/man pages on a wide range of unix systems... in bash, I could build all possible paths开发者_开发技巧 by doing this:

# default search paths
default=$(echo /usr/{share, local, dpkg, XR11}/man/{man, cat}{1..8})

for path in $default
do 
  ...
done

Unfortunately, I'm forced to use sh... I could build the paths with loops, but this would look really ugly... is there a neater/shorter way?


On my Linux system, "man -w" prints the location of the nroff source file instead of its contents. That way you use man's internal search to find the files - just like a user on the command line.

See "man man" for more information.


This isn't terrible:

for dir in share local pdkg XR11; do
  for type in man cat; do
    for n in 1 2 3 4 5 6 7 8; do
      path="/usr/${dir}/man/${type}$n"
      # ...
    done
  done
done

or even, though it's not DRY, this is explicit and readable

prefixes="
    /usr/share/man/man /usr/share/man/cat
    /usr/local/man/man /usr/local/man/cat
    /usr/pdkg/man/man  /usr/pdkg/man/cat
    /usr/XR11/man/man  /usr/XR11/man/cat
"
for prefix in $prefixes; do
  for n in 1 2 3 4 5 6 7 8; do
    path="${prefix}$n"
    # ...
  done
done


you can use find

find /usr/share /usr/local /usr/dpkg /usr/XR11 -type d \( -name "man[0-9]" -o -name "cat[0-9]" -o -name "cat" -o -name "man" \) 


Given Bourne shell, I'd probably use:

for path in `ls -d /usr/*/man/[mc]a[tn][1-8]`
do 
    if [ -d $path ]
    then ...
    fi
done

This only leads you astray if somebody malicious creates a directory /usr/spoof/man/man3, or /usr/share/man/mat1 or /usr/dpkg/man/can2, which is unlikely enough that I'd not worry about it.


You can use find:

find /usr/share /usr/local /usr/dpkg /usr/XR11 -type d -regex '.*/man/\(cat\|man\)[1-8]$'

On my system, I have a bunch of localized man pages that would get excluded by the above, so you could do this:

find /usr/share /usr/local /usr/dpkg /usr/XR11 -type d -regex '.*/\(cat\|man\)[1-8]$'
0

精彩评论

暂无评论...
验证码 换一张
取 消