4

I am unable to run python script within a shell script. Following is the script.sh file where 3 arguments are being passed to the python script.

python main.py 1.0 0.1 10

The python program is not reading the arguments. When same line is run in command line terminal, it runs fine.

<<>> I just checked that by defining the arguments in main.py hardcoded and running the command without arguments. It still doesn't work. The main.py is using some other files also which are located in the same directory. Also, the script is also located in the same directory.

  • main.py is a complicated program but it runs totally fine when executed directly with the terminal and with the same arguments. – hasnain rehman Jun 12 '16 at 7:06
  • 1
    What errors are you getting when put inside the shell script? Is the shell script and the python script are on the same directory? – heemayl Jun 12 '16 at 7:08
  • Yes, the shell script and the python script are in same directory. The error looks like this: ` Traceback (most recent call last): File "main.py", line 2, in <module> from cell import Cell File "/home/hasnain/Data/Studies/Courses/Current_Courses/Seminar/Code/Experiments/Without_LTS1/cell.py", line 9 def __init__(self, width:float, h:float=0.0, hu:float=0.0, hv:float=0.0, flux_r=array((0.0, 0.0, 0.0)), flux_l=array((0.0, 0.0, 0.0)), dt_cfl=0.0, kmax=0.0, k = 0.0): ^ SyntaxError: invalid syntax` – hasnain rehman Jun 12 '16 at 7:14
  • 1
    Can you post the content of script.sh in full? – heemayl Jun 12 '16 at 7:20
  • I have tried it both ways, with nested loops and with hardwired argumenrs In nested loops, it is like: ` arr_t=("1.0") arr_dt=("0.00158") arr_np=("10") for t in "${arr_t[@]}" do for dt in "${arr_dt[@]}" do for np in "${arr_np[@]}" do python main2.py $t $dt $np echo $t echo $dt echo $np done done done` Without loops it was: python main.py 1.0 0.1 10 It does not work either way. – hasnain rehman Jun 12 '16 at 7:33
2

From the SyntaxError you have pasted, it sounds like you are running the script under the wrong version of python, specifically Python 2 instead of Python 3 (because the SyntaxError may be complaining about the annotations, which only exist in Python 3).

If you read the error you actually got, you can see that this really doesn't have anything to do with passing arguments (the title of your question) at all.

The correct solution is to not invoke the script by prefixing it with the interpreter name, but to let it invoke its own interpreter:

./main.py 1.0 0.1 10

instead of

python main.py 1.0 0.1 10

If it's properly written, the script will have a shebang line at the start that specifies what interpreter it needs to run under, and that will be python3 or something similar.

By letting the script specify its own interpreter, the calling script need not know or care which interpreter is required. The script can even be rewritten to use a different language altogether (or it can be replaced with a compiled binary that isn't a script at all) and the callers don't have to be modified to call it differently.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.