Scripts aanroepen vanuit scripts (Bash)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

Manieren om vanuit een Bash-script een ander script aan te roepen Stackoverflow:

  1. Make the other script executable, add the #!/bin/bash line at the top, and the path where the file is to the $PATH environment variable. Then you can call it as a normal command
  2. Call it with the source command (alias is .) like this: source /path/to/script
  3. Use the bash command to execute it: /bin/bash /path/to/script.

The first and third methods execute the script as another process, so variables and functions in the other script will not be accessible. The second method executes the script in the first script's process, and pulls in variables and functions from the other script so they are usable from the calling script.

In second method, if you are using 'exit' in second script, it will exit the first script as well. Which will not happen in first and third methods.

→ Ik heb prima ervaringen met source. Wel belangrijk dat de working directory ok is, of dat je absolute padnamen gebruikt.

Casus: Array in een apart bestand

  • In bestand load_site_array.sh wordt een array geladen
  • In bestand test.sh wordt middels source load_site_array.sh die array ingeladen. Deze array is beschikbaar in dit tweede script.