2

I`m trying to execute a bash script using PHP, but the problem is that the script needs to have some commands and informations inputed during the execution process.

This is what I`m using

$old_path = getcwd();
chdir('/my/path/');
$output = shell_exec('./script.sh');
chdir($old_path);

The script execute OK, but I`m not able to input any option on the script.

Paul Mark
  • 189
  • 12
  • Does the script accept arguments? – segFault Aug 21 '16 at 01:07
  • No, arguments are not accepted on the script, the script runs and ask for a input like, type the name, and for a lot of other inputs – Paul Mark Aug 21 '16 at 01:17
  • Possible duplicate of [PHP, shell\_exec and an input](http://stackoverflow.com/questions/11737251/php-shell-exec-and-an-input) – LF00 Aug 21 '16 at 01:53
  • It`s not the same, since the command is passed on the line, and in my problem the input can be diferent by the script requested, so the inputs cannot be passed before the execution, and during the execution. – Paul Mark Aug 21 '16 at 02:08
  • Possible duplicate of [passing arguments to an interactive program non interactively](http://stackoverflow.com/questions/14392525/passing-arguments-to-an-interactive-program-non-interactively) – tripleee Aug 21 '16 at 11:47

2 Answers2

0

shell_exec() and exec() are not able to run interactive scripts. For that you need a real shell. Here is a project that gives you a real Bash Shell: https://github.com/merlinthemagic/MTS

//if the script requires root access, change the second argument to "true".
$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', false);

//What string do you expect to show in the terminal just before the first input? Lets say your script simply deletes a file (/tmp/aFile.txt) using "rm". In that case the example would look like this: 

//this command will trigger your script and return once the shell displays "rm: remove regular file"

$shell->exeCmd("/my/path/script.sh", "rm: remove regular file");

//to delete we have to press "y", because the delete command returns to the shell prompt after pressing "y", there is no need for a delimiter.  

$shell->exeCmd("y");

//done

I am sure the return of the script is far more complex, but the example above gives you a model for how to interact with the shell.

I would also mention that you might consider not using a bash script to perform a sequence of events, rather issue the commands one by one using the exeCmd() method. That way you can handle the return and keep all error logic in PHP rather than splitting it up between PHP and BASH.

Read the documentation, it will help you.

MerlinTheMagic
  • 575
  • 5
  • 16
0

proc_open() makes this possible without any external libraries:

$process = proc_open(
    'bash foo.sh',
    array( STDIN, STDOUT, STDERR ),
    $pipes,
    '/absolute/path/to/script/folder/'
);

if ( is_resource( $process ) ) {
    fclose( $pipes[0] );
    fclose( $pipes[1] );
    fclose( $pipes[2] );
    proc_close( $process );
}
Ian Dunn
  • 3,541
  • 6
  • 26
  • 44