简体   繁体   中英

how to send a variable from php to a .sh file

Is it possible to send a variable from a php script to a shell script?? The code is given below"

exec('./test.sh');

I want to send a variable called $path to test.sh file.and recieve this variable in test.sh file and want to use this value.so how do I get the value in that test.sh file..Is there have any solution for that..?? thanks in advance..

Sure, you can pass variables the same way as you do on the commandline. Just append them. For safety, escape the variable before you do so:

exec('./test.sh ' . escapeshellarg($path));

Then, in your shell sccript, this is available as $1 :

#!/bin/sh
echo $1

There are several ways to do that:

Pass as argument to the script:

<?php
exec("./test.sh ". escapeshellarg($path));

And from the script:

#!/bin/bash
echo "Path is $1"

Pass through the environment

<?php
putenv('PATH='. $path);
exec("./test.sh");

And from the script:

#!/bin/bash
echo "$PATH"

On the standard input

I guess there is a way to do this too, but I wasn't able to find out how on a quick search.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM