Print array value in bash by PHP shell_exec

2

I have another script, but I simplified it to simulate the problem that persists.

test.sh

#!/bin/bash
ARRAY=('like' 'a' 'stone')
echo ${ARRAY[0]}

In the file below, I have already made sure that shell_exec is actually executing the above command.

index.php

<?php
$comando = file_get_contents("teste.sh");
echo "<pre>";
var_dump(shell_exec($comando));
echo "</pre>";

Here I am expecting you to print like , but the result always comes null as below.

Output

NULL

However, if I change my teste.sh to only php -v , it prints the version of my PHP. Which is not what I want, but it serves as a stupid strip.

Output

string(235) "PHP 5.5.9-1ubuntu4.17 (cli) (built: May 19 2016 19:05:57) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
"

Then I get confused, like he is not able to print what I'm hoping for? What am I doing wrong?

    
asked by anonymous 31.07.2016 / 21:16

1 answer

3

You have not done anything wrong.

This problem happens because script is interpreted differently in two different environments.

When running ./teste1.sh the system will look at shebang , which in this case is #!/bin/bash e will run the script using /bin/bash . If you execute it by typing: sh script1.sh it will execute using /bin/sh .

In Ubuntu, /bin/sh is typically a shortcut to /bin/dash (see: ls -l /bin/sh ) a Bourne shell that does not support arrays , and this is the source of the problem.

When you open a new terminal window

01.08.2016 / 04:34