Shellscript does not run cd, calling sh via php

Asked

Viewed 75 times

0

all right? See if anyone has been through this, I’m with this simple code in shellscript, which access folder (/home/test4toquew5824) and then confirm if I entered the directory with pwd,

#!/bin/bash
cd /home/teste4toquew5824 && pwd

and I have a php file (located in /var/www/html) that calls this sh:

    $retorno = shell_exec('/usr/local/bin/api/teste.sh');
    echo "<br><br>";
    var_dump($retorno);

When I get the pwd return it shows as if it is still in the php file directory (/var/www/html), i.e., it is not "accepting" or running the cd command.

1 - I’ve already applied permissions for php and sh.

2 - I have confirmed the directory /home/teste4toquew5824 exists

3 - I already tested and placed an echo inside sh just to confirm that php is calling and running sh correctly.

4 - I have already put in sh a line to copy a file just to be sure that another command is running too and copied the file to /var/www/html

Anybody got any ideas? Thank you very much

  • Change the shebang from the script of #!/bin/bash for #!/bin/sh - .Bash does not accept pwd.

  • I switched here, even though it didn’t happen :( It doesn’t seem to be entering the folder

3 answers

0

Try adding an echo to the script by staying that way.

cd /home/teste4toquew5824 && echo $(pwd)

  • nothing saw, continues printing NULL :(

0

Try changing to:

$return = shell_exec('. /usr/local/bin/api/test.sh');

This forces you to execute the command within your current process.

  • I tested it here now, it didn’t roll :(

  • You can change the current process execution directory using chdir, for example: chdir(' /home/teste4toquew5824'); .

-1

Hello... Try this way: Throw the path into a variable. By the way, I don’t know which user will run so load the profile and externally to the script, confirm if you have permission.

Then run the cd calling the variable, example:

#!/bin/bash
. /etc/profile
path=/home/teste4toquew5824
cd $path

Browser other questions tagged

You are not signed in. Login or sign up in order to post.