Send PHP variable to Shell Script

Asked

Viewed 2,128 times

3

I have a file .sh and it has variables that I need to receive from PHP, follow an example:

//variavel 1 que preciso receber externamente
NAME=""
//variavel 2 que preciso receber externamente
DIR=""

tar -zcf $HOME.tar.gz $DIR

The file call by PHP serious like this:

shell_exec('sh arquivo.sh');

I just need to know how to send the data of the 2 variables.


Another way would be to send the whole script directly via php shell_exec(), how would I send a script of that kind?

(That doesn’t work):

shell_exec('VAR=tar -zcf teste.tar.gz teste/testeDir
if [ "$SUCCESS" == "0" ]; then
   echo "Sucesso!"
else
   echo "Sucesso!"
fi');
  • I don’t know if it helps, but by PHP itself you can run bash commands by adding variables and picking up the return, in this way it would dispense with the need for this external bash file.

  • Just one thing: JS is client-side! I don’t understand why JS is involved in this.

  • I traveled, I thought to do with ajax, but I was going to use php of the same...

  • Man this doesn’t work? echo "{$HOME}"; and echo "{$DIR}"; ?

  • I don’t understand Ivan, how do I use it ? I read something about sending the variable like this: $page = shell_exec('my_script.sh "{$var1}" "{$var2}"'); but I don’t know if it’s right and I can’t catch it in sh

  • kind of: NAME="$(ls -1)". echo "{$NAME}"

Show 1 more comment

3 answers

4


Pass the data through arguments, which can be read in bash scripts over $1, $2, etc. Your bash script would look like this:

#!/bin/bash
NAME="$1"
DIR="$2"

tar -zcf $NAME.tar.gz $DIR

In PHP, enter the arguments in their order:

shell_exec('sh seu_script.sh nome_qualquer diretorio_qualquer');
  • tb didn’t work!

  • Ok, first check if the problem is in the bash script or in PHP. Running on the terminal, the bash script goes well?

  • sorry, I think it was cache, it worked!

  • In PHP, be sure to inform the full path of the bash script.

  • only had a problem with the php variable q so $test = 'teste2'; $output = shell_exec('sh .. /script.sh '.$test);

1

When performing the function shell_exec it is possible to pass variable values as arguments to the command that will be executed:

$arquivo = "teste";
$dir = "testeDir";

$resultadoExec = shell_exec("sh script1.sh $arquivo $dir");

echo $resultadoExec;

To receive information in the archive .sh use the special variables $1 and $2:

#!/bin/bash

arquivo=$1".tar.gz"
dir=$2

# Verifica se o script recebe os argumentos
if [ $# -ne 2 ] then
   echo "Atenção! É necessário informar o arquivo e diretório de destino!"
   echo "Exemplo: \n $0 <nomeArquivo> <diretorio>"
   exit
fi

tar -zcf $arquivo $dir
if [ $? -eq 0 ] then
   echo "Arquivo descompactado com sucesso no diretório: $dir"
else
   echo "Erro! Não foi possível realizar a operação! Tente novamente"
fi
  • 1

    did not work either php or sh

  • was cache, is probably working!

0

In the shell file script.sh do the following:

#!/bin/bash

NAME='filename'
DIR='path'

echo $NAME
echo $DIR
tar -zcf $NAME.tar.gz $DIR

In PHP do so:

$file = "script.sh";
if (file_exists($file)) {
   chmod($file, "go+x");
   $string = shell_exec('sh '.$file);
   $data = explode("\n", $string);
   //Nome: filename - Diretório: path
   echo 'Nome: '. $data[0] . ' - Diretório: ' . $data[1]; 
} else {
  echo 'arquivo  não existe';
}

If you need to pass parameters in the shell:

  #!/bin/bash

    NAME=$1
    DIR=$2

    echo $NAME
    echo $DIR
    tar -zcf $NAME.tar.gz $DIR

In PHP:

 $file = "script.sh";

   $params = array('filename', 'path');
   $param = ' '.implode(' ', $params);
    if (file_exists($file)) {
       chmod($file, "go+x");
       $string = shell_exec('sh '.$file.$param);
       $data = explode("\n", $string);
       //Nome: filename - Diretório: path
       echo 'Nome: '. $data[0] . ' - Diretório: ' . $data[1]; 
    } else {
      echo 'arquivo  não existe';
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.