You can pass variable from bash to php

1

I have a question that I do not know if it is not possible or that I do not know how it is done. If I have this code php:

<?php
$salida = shell_exec('bash prueba.sh');
echo "<pre>$salida</pre>";
exit;
?>

and this in bash:

#!/bin/bash
patch="una_ruta_cualquiera"

lista=$(ls -lt $patch | grep ^d | awk '{print $9}')
for carpeta in $lista; do
    #echo $carpeta
    listar=$(ls -l $patch/$carpeta | grep -v ^d | awk '{print $9}')
    for archivos in $listar; do
        echo "$carpeta/$archivos"
    done
done

Can I do it in some way in which the result ( echo "$carpeta/$archivos" ) arrived at the php so that it was a $ variable in php and I could work with it?

    
asked by juan 15.12.2016 в 01:34
source

2 answers

1

The value you get in the variable $salida can be passed to an array from a wildcard that concatens in the echo that you print in the bash, so you can separate the line.

In the bash:

#!/bin/bash
patch="una_ruta_cualquiera"

lista=$(ls -lt $patch | grep ^d | awk '{print $9}')
for carpeta in $lista; do
    #echo $carpeta
    listar=$(ls -l $patch/$carpeta | grep -v ^d | awk '{print $9}')
    for archivos in $listar; do
        echo "$carpeta/$archivo-\$-"
    done
done

In the php:

$salida .= shell_exec('bash prueba.sh');
$a_salida = explode("-$-",$salida);

foreach ($a_salida as $valor) {
    echo $valor."<br />";
}

corrected

    
answered by 16.12.2016 / 02:51
source
0

What I would like in your case would be to save the value of the bash variable in a file and then from the php with a fopen would read the file and pass the value to a php variable .

    
answered by 15.12.2016 в 01:43