3

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?

7
  • 2
    Is it in the same directory? Commented Apr 27, 2015 at 13:00
  • What value does the Bash script add? Commented Apr 27, 2015 at 13:01
  • You may want to put the full qualified path. Commented Apr 27, 2015 at 13:01
  • Why not just do: echo shell_exec('python /home/administrator/Desktop/name.py');? Commented Apr 27, 2015 at 13:01
  • Try giving the full path in the shell script to the directory Commented Apr 27, 2015 at 13:02

2 Answers 2

2

try adding the line before executing python script

cd "$(dirname "$0")"

in your bash file?

Sign up to request clarification or add additional context in comments.

2 Comments

Both python and bash files are on the desktop, so I have added cd "$(Desktop "$0")" This is the new error:/home/administrator/Desktop/Helloworld.sh: 3: /home/administrator/Desktop/Helloworld.sh: Desktop: not found python: can't open file 'name.py': [Errno 2] No such file or directory
no, just add cd "$(dirname "$0")" don't change dirname leave it
1

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.

2 Comments

Both python and shell scrip are on the desktop, I have changed my shell script to:#!/bin/bash BASEDIR=Desktop "${0}" cd "$BASEDIR" python name.py................................but no luck
It's BASEDIR=dirname "${0}" not BASEDIR=Desktop "${0}", dirname is a command, you don't need to modify my code, just copy and paste

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.