0

I got a shell variable holding this path

/usr/share/man/man1/bitmap.1

There COULD be a file named bitmap.1, but it could also be bitmap.1.z, bitmap.1.gz, bitmap.1.Z.

Now I want to pass the real file to a function while at the same time resolving the correct file, like

function process {
    # do something
}

path="/usr/share/man/man1/bitmap.1"

process ${path}.(z|Z|gz) # here the shell shall resolve the real filename

Is there a way to do it elegantly or to I have to test if bitmap.1, bitmap.1.z etc. exit and then pass that file to the function???


Forget to mention I'm restricted to the Bourne-Shell, so no Brace Expansion at hand -,-.

4 Answers 4

1

You can still do the globbing in Bourne Shell

for f in ${given-path}*; do
process $f
done

It's a good idea to do as much of your shell scripting as possible limiting to Bourne shell syntax; that maximizes portability. In many cases, it also improves speed.

3

If you know the list of possible extensions, you can do:

process ${path}{,.Z,.Z,.gz}
1

I believe Christian is on the right track. If you are certain of at least a portion of your path, using Bash's builtin globbing will yield any files matching your pattern.

For example, I have a function in my .profile which I frequently use to cat gzipped and plain-text log files:

function mscat {
  is_gzip="\.gz$"

  for file in $@; do
    if [[ "$file" =~ $is_gzip ]]; then
      zcat $file
    else
      cat $file
    fi
  done
}

Which I would call like this:

mscat /var/log/ncftpd/sess.2010*

Because globbing can yield more than one result, your 'process' function should be capable of acting upon multiple arguments. In my example, you can see how I do this by iterating over each argument in $@.

0

when you only have one file you can use bash globbing process ${path}*

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .