I have the following file:
$ cat my_exec.sh
#!/usr/bin/env python
print(10)
It should just print 10. However, I can't get it to do so:
$ sudo ./my_exec.sh
sudo: ./my_exec.sh: command not found
$ sh my_exec.sh
my_exec.sh: line 3: syntax error near unexpected token `10'
my_exec.sh: line 3: `print(10)'
How do I run my file?
3 Answers 3
You can run it via the python command:
$ python my_exec.sh
To run it as simply ./my_exec.sh, you need to make the file executable first:
$ chmod 755 my_exec.sh
Also note that by convention python files end in .py .
Comments
- Change the shebang to
#!/usr/bin/env python - Change the filename to my_exec.py, as is convention for python files
- You can run with
python my_exec.py - You can
chmod +x my_exec.pyand then./my_exec.py
Comments
You must enter the directory that you have saved you file through the cmd with cd command. After that you just execute the file with : python name_of_the_file.py . But first you must make it executable with chmod command
For example if you have saved your file at Desktop with the name mycode.py :
cd Desktop
chmod +x mycode.py
python mycode.py
my_exec.pyand update the first line to#!/usr/bin/pythonsudomeans "super user do". Do you need your script to be executed with elevated/different permissions? If not, don't usesudo.shexecutes a shell script in POSIX-compliance mode, so you don't want that either. Have you tried./my_exec.sh?sudobecause simply doing./my_exec.shresulted in permission denied.