1

Background

I have a program prog that takes input from stdin and does something to it. It also takes a flag -f FILE to find a datafile used during operation.

I wish to create a bash script to act as a wrapper around this program so that this flag need not be used. So I wrote the following script progwrap.sh:

#!/bin/bash

/path/to/prog -f /path/to/data

which does the trick for basic functionality, so that I can do something like

$ cat somedata.txt | progwrap.sh

and get what I want.

Goal

Now, it turns out that prog also takes some other flags of the form -h, -u, etc... I want to be able to pass these to progwrap.sh so that they are in turned passed to prog. So I amended progwrap.sh as follows:

#!/bin/bash

/path/to/prog -f /path/to/data "$@"

allowing it to take arguments and supposedly feeding them to prog within the script.

Problem

However this somehow doesn't work, in that:

$ cat somedata.txt | progwrap.sh -h

produces the exact same output as

$ cat somedata.txt | progwrap.sh

which is not the correct behaviour (i.e. the output should come out in a different format as a result of the -h flag being passed to progwrap.sh).

In fact, feeding progwrap nonsense arguments for which prog has no support, and which usually cause prog to throw an error, does nothing. I conclude that the arguments passed to progwrap.sh are ignored by the call to prog within the script.

Question

What am I doing wrong? How can I pass flags to progwrap.sh so that these flags are then passed to prog within my script?

Edward Grefenstette
  • 603
  • 1
  • 7
  • 20

1 Answers1

1

Thanks to user melpomene for helping. It turns out the above script is fine for passing flags, and the fault lie with prog ignoring all flags after -f File. The script works fine when modified to:

#!/bin/bash

/path/to/prog "$@" -f /path/to/data

Thanks!

PS. In my defence this flag behaviour was not documented by the designers of prog.

Jonah Bishop
  • 12,279
  • 6
  • 49
  • 74
Edward Grefenstette
  • 603
  • 1
  • 7
  • 20
  • In `prog`'s defense, that behavior is actually quite standard: first come all options, then all other arguments. One of the benefits of this approach is, if you need an actual argument that happens to start with `--`, you can use the special `--` option to indicate "end of options". (For example, `grep -- --foo bar.sh` will search the file `bar.sh` for lines containing the string `--foo`.) – ruakh Nov 29 '12 at 14:39