3

Given two scripts

foo.sh
bar.sh

copied under /Contents/Resources in an .app bundle, where foo.sh

#!/bin/bash
. ./bar.sh
echo $1

Do get an error of

No such file or directory

on the line where the script tries to source bar.sh

Is there a way to relatively reference bar.sh?

Is there another way to bundle a set of bash scripts in a .app?

qnoid
  • 2,346
  • 2
  • 26
  • 45

2 Answers2

2

What you can do is:

  1. get the full path & directory where the actual running script (your foo.sh) is stored. see https://stackoverflow.com/a/246128/3701456
  2. call or source the second script (your bar.sh) with directory from 1. (or relative to that directory)

Simple Example:

$cat script2
#! /usr/bin/env bash
echo "Hello World, this is script2"

$cat script1
#! /usr/bin/env bash
echo "Hello World from script 1"
echo "Full script path: $BASH_SOURCE"
echo "extracted directory: $(dirname $BASH_SOURCE)"
echo "running script 2"
$(dirname $BASH_SOURCE)/script2 && echo "running script 2 successful" || echo "error running script 2"
echo "sourcing script 2"
source $(dirname $BASH_SOURCE)/script2 && echo "sourcing script 2 successful" || echo "error sourcing script 2"

Test:

$ls /tmp/test
script1  script2
$pwd
/home/michael
$/tmp/test/script1
Hello World from script 1
Full script path: /tmp/test/script1
extracted directory: /tmp/test
running script 2
Hello World, this is script2
running script 2 successful
sourcing script 2
Hello World, this is script2
sourcing script 2 successful

See link above for more in detail discussion ...

Community
  • 1
  • 1
Michael Brux
  • 4,256
  • 1
  • 20
  • 21
0

Old question but to address the last part (reflecting the current Apple guidelines): yes, you should definitely place all executables (including scripts) in the MacOS sub-folder of your bundle:

MacOS - (Required) Contains the application’s standalone executable code. Typically, this directory contains only one binary file with your application’s main entry point and statically linked code. However, you may put other standalone executables (such as command-line tools) in this directory as well.

(Source: Anatomy of a macOS Application Bundle.)

Breaking these rules will prevent you from successfully signing your app bundle for Gatekeer (and, of course, macOS Notarization).

The first part was adequately handled by the other response.

elder elder
  • 645
  • 1
  • 9
  • 23