0

I have two scripts which transform the code from software v.1 to software v.2 and viceversa. These are essentially a lot of sed substitutions.

In the way I wrote them they accept two arguments: first, the input file , and second the output file.

$ ./transform2new oldfile newfile

or

$ ./transform2old newfile oldfile

I'd like to merge both codes and manage the transformation with a flag, e.g,

$ ./transform -n oldfile newfile
$ ./transform -o newfile oldfile

Question

How could I write a single script to achieve the purpose?

Cheers.

1 Answer 1

2

Put the contents of each script in a function and you are done. Treat these functions as if they were your scripts (you can start the functions with arguments). E.g.

function transform2new() {
  # here the code from script one
}

function transform2old() {
  # here the code from script two
}

# here comes the new super script, that reads the arguments and the option:
if [ "$1" = "-n" ]; then
  transform2new "$1" "$2"
elif [ "$1" = "-o" ]; then
  transform2old "$1" "$2"
fi
1
  • 1
    The parameters for the function calls should be "$2" "$3" instead of "$1" "$2".
    – Adaephon
    Feb 19, 2014 at 20:02

You must log in to answer this question.

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