简体   繁体   中英

Shell script not executing a python file

I am trying to run a script which in turn should execute a basic python script. This is the shell script:

#!usr/bin/bash
mv ~/Desktop/source/movable.py ~/Desktop/dest
cd ~/Desktop/dest
pwd
ls -lah
chmod +x movable.py
python movable.py
echo "Just ran a python file from a shell script"

This is the python script:

#!usr/bin/python
import os
print("movable transfered to dest")
os.system("pwd")
os.system("mv ~/Desktop/dest/movable.py ~/Desktop/source")
print("movable transfered to dest")
os.system("cd ~/Desktop/source")
os.system("pwd")

Q1. The shell script is not executing the python file. What am I doing wrong? Q2. Do I need to write the first line #!usr/bin/python in the python script? Thank you.

You are missing a '/' in the shebang line:

#!usr/bin/python

should be

#!/usr/bin/python

Another thing I noticed was, you are calling cd in os.system. Since that command would be executed in a sub shell, it won't change the current directory of the calling script process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM