1

I am writing an upstart configuration file. Within it, I have a pre-start script, a script, and a pre-stop script stanza. Each of these had a large amount of identical code. Thus, I attempted to refactor this code into a few bash functions. However, in so doing I discovered that "one does not simply write bash in an upstart configuration file". A bash function keyword is not allowed as it is interpreted as a stanza, and it isn't a valid upstart stanza.

# Some header stanzas that are boring
...
env ONE_OF_A_FEW_VARIABLES
...
function an_illegal_function () {...}

pre-start script
  function_call_A
  function_call_B
end script

script
  function_call_A
  function_call_C
  echo "I love pie."
end script

pre-stop script
  function_call_B
  function_call_C
end script

I would really like to avoid the kind of code duplication that will exist if I have to copy-paste the contents of each function into stanzas like those above. How can I get some bash commands DRY'd up into a common location and have each of my *script stanzas reference them?

BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
  • I'm not sure you can do this in the configuration file itself. Instead, define the functions in a separate file, and source that file from each script. – chepner Oct 25 '16 at 19:29
  • Forgive me @chepner but could you either clarify what you mean or link to something that explains what you mean by "source"-ing that file? – BlackVegetable Oct 25 '16 at 19:36
  • `. /etc/my_app/my_library` or `source /etc/my_app/my_library`, where `/etc/my_app/my_library` is the file in which you define the common functions. – chepner Oct 25 '16 at 19:39
  • Will that allow me to mutate environment variables from within the source'd script? (I only realize now that I ought to include that requirement in the question.) – BlackVegetable Oct 25 '16 at 19:44
  • Turns out upstart configuration files support almost no bash syntax, so including "functions" there is a moot point. I'm not sure what to do with this question, given that information. – BlackVegetable Oct 29 '16 at 15:28

0 Answers0