1

I'm having problems with scripts that originate from the function dependencies order.

If a function calls another function below it, it errors. I have to change the order of the functions to get it to work.

I like to keep my pipeline function at the top of my script. This serves as a table of contents for the script. All I have to do is look at the pipeline, and I have an eagles eye view of the script.

Is there anyway to do this in bash?

Is there a flag that would tell the script to read the entire file before executing the pipeline?

Example:

#!/bin/bash

# Pipeline syntax example.

# Pipeline
execute(){
    lay_the_foundation
    prime_the_masses
    manufacture_fear
    offer_the_solution
    hedge_the_fallout
    trillion_dollar_payday
    post_operation_wetworks
    rinse_repeat
}

execute # This doesn't work, because of command not found.

lay_the_foundation(){
    echo 'Co-opt the media.'
}

prime_the_masses(){
    echo 'Prime the slaves with media assets.'
}

manufacture_fear(){
    echo 'Manufacture a crisis.  Use media assets to keep the slaves in fear.'
}

offer_the_solution(){
    echo "Give the slaves a crisis 'cure' for 'free', paid for by their extortions (taxes)."
}

hedge_the_fallout(){
    echo "Setup the fall guys to promote the 'cure'."
}

trillion_dollar_payday(){
    echo 'Ride the money train until it runs out of steam.'
}

post_operation_wetworks(){
    echo 'Throw the fall guys to the wolves to appease the slaves'
}

rinse_repeat(){
    echo 'Wait until the slaves forget or die.  Rinse repeat.'
}

execute  # This works.
Emily
  • 2,129
  • 3
  • 18
  • 43

1 Answers1

2

I'd recommend using an extra file, which you can source in the pipeline script.


For example, define an functions.sh like so:

#!/bin/bash

lay_the_foundation(){
    echo 'Co-opt the media.'
}

prime_the_masses(){
    echo 'Prime the slaves with media assets.'
}

manufacture_fear(){
    echo 'Manufacture a crisis.  Use media assets to keep the slaves in fear.'
}

offer_the_solution(){
    echo "Give the slaves a crisis 'cure' for 'free', paid for by their extortions (taxes)."
}

Then use the pipeline.sh like so:

#!/bin/bash

source /private/tmp/test/functions.sh

# Pipeline
execute(){
    lay_the_foundation
    prime_the_masses
    manufacture_fear
    offer_the_solution
}
execute

Will result in :

./pipeline.sh
Co-opt the media.
Prime the slaves with media assets.
Manufacture a crisis.  Use media assets to keep the slaves in fear.
Give the slaves a crisis 'cure' for 'free', paid for by their extortions (taxes).
0stone0
  • 34,288
  • 4
  • 39
  • 64