简体   繁体   中英

Running a python script within a bash file with php

I am trying to run a simple python script (name.py):

#!/usr/bin/env python 
 name = raw_input('What is your name?\n')
 print 'Hi, %s.' % name

with the following bash script(Helloworld.sh):

 #!/bin/bash

python name.py

through the following php

<!DOCTYPE html>
<html>

<body>

<?php
if (isset($_POST['Submit1'])) {

 echo shell_exec('sh /home/administrator/Desktop/Helloworld.sh');
}

  ?>
<form action="myfilename.php" method="post">
<p><Input Type = "Submit" Name ="Submit1" Value = "Save Parameters">

</form>
</p> 

</body>
</html>

The error in the log is:

python: can't open file 'name.py': [Errno 2] No such file or directory

The bash file works fine from the terminal. What should I do?

try adding the line before executing python script

cd "$(dirname "$0")"

in your bash file?

You can modify the shell script like this:

#!/bin/bash
BASEDIR=`dirname "${0}"`
cd "$BASEDIR"
python name.py

to always be run on the directory containing the script. Or if the name.py is in another directory, then change the cd command accordingly.

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