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


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


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


1 Answer 1


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"
  • 1
    The parameters for the function calls should be "$2" "$3" instead of "$1" "$2".
    – Adaephon
    Commented 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 .