Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

4 Answers 4

up vote 1 down vote accepted

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.

share|improve this answer

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

process ${path}{,.Z,.Z,.gz}
share|improve this answer

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.