mlenthusiast
mlenthusiast

Reputation: 1194

Bash script to clone Git repo and run another bash script

I'm having some issues with PYTHONPATH with a bash script I'm trying to run. There's 2 bash scripts, clone_and_run.sh is in a root folder. This is cloning a remote Git repo and then running another bash script in the root of that Git repo.

However, after cloning the repo and executing run.sh, that second script cannot run the Python scripts in the repo. But if I go into the repo root and execute run.sh, it works fine.

Here's the folder structure:

root
|----clone_and_run.sh
|----repo(after cloning from remote)
    |----source
         |----script.py
    |----run.sh
clone_and_run.sh:

#!/usr/bin/env bash
set -e

PYTHONPATH=`pwd`./repo/:$PYTHONPATH

git clone https://---------(redacted)

bash ./repo/run.sh
#!/usr/bin/env bash
set -e

PYTHONPATH=`pwd`/../repo:$PYTHONPATH

python ./source/script.py

This is the error I get when I execute clone_and_run.sh: python: can't open file './source/script.py': [Errno 2] No such file or directory

After cloning, if I go into repo and execute run.sh directly, it works fine. I tried different PYTHONPATH on both scripts, no luck.

Upvotes: 0

Views: 1164

Answers (1)

jthill
jthill

Reputation: 60255

You didn't cd in, the current working directory . when run.sh starts isn't the script's directory, it's wherever the invoker's running.

To find an invoked script's directory my usual method is readlink -f $0.

Upvotes: 1

Related Questions