Maritn Ge
Maritn Ge

Reputation: 1216

Why can't my terminal script execute/find my python script?

I am using a Linux terminal app, it works flawlessly but I think I am having a problem here, and I don't know if it's an app or a me-error

below is my test.sh, everything in #comment is what i get after each line is called/runs, but note that this would normally be 2 files i guess, just brought them together

#!/bin/bash

echo "this is a test"
 # this is a test
ls
 # code downloads
pwd
 # /data/data/com.termux/files/home
ls code
 # python
ls code/python
 # scripts programs
ls code/python/scripts
 # abc.py test.py
cd code/python/scripts
pwd
 #/data/data/com.termux/files/home/code/python/scrips
ls
 # abc.py test.py
python3 test.py
 # hello world
cd
python3 code/python/scrips/abc.py
 # python3: can't open file '/data/data/com.termux/files/home/code/python/scrips/abc.py': [Errno 2] No such file or directory
python3 code/python/scrips/test.py
 # python3: can't open file '/data/data/com.termux/files/home/code/python/scrips/test.py': [Errno 2] No such file or directory

important to note is that if i run python3 code/python/scrips/abc.py in home, it works though. this is all i could do with testing and trying stuff out myself, all you see above is me "debugging"

I've seen shell programming questions here before, so I think it's an ok question.

Upvotes: 0

Views: 1489

Answers (1)

Eamonn Kenny
Eamonn Kenny

Reputation: 2052

This is a path problem. After you run cd just before running the two python3commands you need to check the path that you return to.

If you were in a subdirectory e.g: sub1

you would then have travelled to sub1/code/python/scripts

but running cd sends you back to $HOME or /home/username.

This means that running python3 with the arguments code/python/scripts/abc.py will always fail because you are not in the correct starting folder.

So the issue is the line cd. This is causing all the problems.

cd && python3 code/python/scrips/abc.py

The cd command is only applied in the context of that signal line. On the next line you will be back in the current working directory.

Upvotes: 1

Related Questions