I would like to execute hello.py using test.sh. However, it seems test.sh read hello.py as sh file.
error message
line 1: syntax error near unexpected token `'hello''
test.sh
#!/user/bin/ python (which python path)
ARRAY=(1 2 3 4)
for num in ${ARRAY[@]}; do
/artic/m-yasunaga/work/korin-3/src/example/hi.py
echo $num"th loop"
done
hello.py
print('hello')
When I changed print('hello') to echo hello, it worked perfectly.
How can I execute hello.py as python code? ( I use linux )
2 Answers 2
You have some syntax errors etc in your scripts.....try this:
Create the following two scripts.
Name: test.sh
#!/usr/bin/env bash
ARRAY=(1 2 3 4)
for num in ${ARRAY[@]}; do
./hello.py
echo $num"th loop"
done
Name: hello.py
#!/usr/bin/env python
print('hello')
From your command line, run the following
chmod 700 test.sh ; chmod 700 hello.py
Now from the command line, you can run:
./test.sh
The output will be:
>./test.sh
hello
1th loop
hello
2th loop
hello
3th loop
hello
4th loop
1 Comment
for num in "${ARRAY[@]}"; do and echo "${num}th loop").You should mention the python path
#!/user/bin/ python (which python path)
in hello.py file and make it executable using
chmod +x hello.py
Now you can use it as a executable.
#!/bin/bashor#!/usr/bin/env bash), not a generic shell shebang (like#!/bin/sh)./user/bin/ python? That's ... bizarre. Try/usr/bin/python.