Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you reference a shell script packaged in your app bundle?

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?

like image 814
qnoid Avatar asked Jan 26 '26 09:01

qnoid


1 Answers

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

like image 159
Michael Brux Avatar answered Jan 28 '26 03:01

Michael Brux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!