4

I have self-writed sh script, which contains contruction like "cd directory"

It is successfully running through terminal

. /path/to/script.sh param1 param2

I want to run this script through PHP

shell_exec('. /path/to/script.sh param1 param2');

shell_exec('. /path/to/script.sh "param1" "param2"');

not running correctly

shell_exec('/bin/bash /path/to/script.sh param1 param2');

running, but directory changing is not working

Please, help. Thank you in advance

5
  • 1
    You'd better use full path instead of using ~ as HOME is different among users. Commented Oct 16, 2013 at 12:28
  • You know that you have more than one difference between the commands you're running, right? Commented Oct 16, 2013 at 12:28
  • @IgnacioVazquez-Abrams Off course Commented Oct 16, 2013 at 13:05
  • in cd directory, is directory a relative path or an absolute one? Commented Oct 16, 2013 at 19:46
  • @geomagas I tried both variants Commented Oct 16, 2013 at 19:48

6 Answers 6

5

You're starting your command with . - that will be interpreted as shell-source command, which is not what you want, obviously. Instead specify full path and do like:

$result = shell_exec('/bin/bash /full/path/to/script.sh param1 param2');
//var_dump($result);

-also, make sure your php user have permission to execute your sh-script and that PHP can use functions like exec() (they could be disabled by configuration)

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

7 Comments

Yeah, script is successfully running, but constructions like "cd my_directory" is not working
What do you want with that cd? What is happening in your script? Why are you not able to use full path inside script instead of doing cd?
cd_one (cd_one is function in my bash_profile) executing some routine operations cd_two (cd_two is function in my bash_profile) executing some routine operations
cd_one in bash_profile is just cd first_directory and cd_two is cd second_directory
Replace your aliases to direct command calls. Also try to use full paths (i.e. get rid of cd at all - normally, its better to avoid them since they cause less-readable code)
|
2

You need to use quotes to send arguments, try this :

$command_result = shell_exec('script.sh "'.$param1.'" "'.$param2."');

Comments

1

Use

shell_exec('./path/to/script.sh param1 param2');

instead of

shell_exec('. /path/to/script.sh param1 param2');

...and make sure your shell script have executable permission for user who is running your script.

Comments

0

This function is disabled when PHP is running in safe mode. Check your php.ini file and make sure safe mode is turned off and it's not in 'disable_functions'.

Comments

0

Aviod:

  1. Using the . notation, which would not work outside a bash shell. Use /bin/bash <yourscript> instead.

  2. Trying to cd to relative paths, as this confines the script's execution to be done from a specific location only

  3. Referencing (or implying) .bash_profile resources, as .bash_profile is executed only for login shells. If applicable, use .bashrc instead.

Comments

0

It's better to use escapeshellarg to escape parameters instead of just adding quotes. escapeshellarg would escape all pram chars not allowed in shell.

Comments

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.