1

I need to get completion variants for Linux commands using PHP function exec(). I try this:

$c = exec('compgen -c pyt');

I except getting something like this

python3.5m
python3.5
python2.7
python2
python3
python3m
python

But instead I get an error:

sh: 1: compgen: not found

When I execute this command directly in terminal output is correct:

omix@omix:~$ compgen -c pyt 
python3.5m 
python3.5 
python2.7 
python2
python3 
python3m 
python

I also tried using function shell_exec() but it didn't work.

hungl
  • 101
  • 8

1 Answers1

2

Finally I have solved this. I found out that I use different shells in my terminal session and when running PHP exec() (or shell_exec()) function:

omix@omix:~$ ps -p $$
  PID TTY          TIME CMD
15471 pts/0    00:00:00 bash
omix@omix:~$ php -a
Interactive mode enabled

php > echo shell_exec('ps -p $$');
  PID TTY          TIME CMD
15908 pts/0    00:00:00 sh

Now I run shell commands from PHP like this:

$c = shell_exec('/bin/bash -c "compgen -c pyt"');

This works for me.

hungl
  • 101
  • 8