melinath
melinath

Reputation: 839

How can I run php -a (interactive shell) from inside a php script?

I'm trying to create a shortcut script for setting some environment variables and the include path before running php -a. So it looks like this:

#!/usr/bin/env php
<?php

putenv("ENVVAR=value");
passthru("php -a -d include_path=<path>");

This mostly works, but the php shell doesn't correctly output its prompt.

I.e. normal output from php -a:

$ php -a
Interactive shell

php > echo "hi\n";
hi
php >

Output from this script:

$ ./myscript.php
Interactive shell

echo "hi\n"; 
hi

Additionally, there is no support for walking through the history (or even right or left along what's already been typed) with arrow keys.

Is there a way to get this to work correctly?

I'm using Mac OS X, PHP 5.4.27. I have already tried redirecting stderr to stdout, in case that was somehow the cause (it wasn't.)

Upvotes: 1

Views: 129

Answers (1)

Kryten
Kryten

Reputation: 15760

Rather than writing it in PHP, why not do it as a shell script.

#!/bin/bash

export ENVVAR=value

php -a -d include-path=whatever

I'm using bash, and it works fine under Ubuntu 12.04. I'm not sure what shell is available on OSX.

Upvotes: 1

Related Questions