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.

5
  • Does the script accept arguments? Commented Aug 21, 2016 at 1: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 Commented Aug 21, 2016 at 1:17
  • Possible duplicate of PHP, shell_exec and an input Commented Aug 21, 2016 at 1: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. Commented Aug 21, 2016 at 2:08
  • Possible duplicate of passing arguments to an interactive program non interactively Commented Aug 21, 2016 at 11:47

2 Answers 2

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.

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

Comments

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 );
}

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.