Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute sh script with parameters

Tags:

bash

shell

php

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

like image 745
indapublic Avatar asked Sep 18 '25 12:09

indapublic


2 Answers

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)

like image 52
Alma Do Avatar answered Sep 20 '25 02:09

Alma Do


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

$command_result = shell_exec('script.sh "'.$param1.'" "'.$param2."');
like image 42
Alexandre Nucera Avatar answered Sep 20 '25 04:09

Alexandre Nucera