Share data between bash and php
We are trying to create a project that will run on linux. But we want to see results from browser. We want to use PHP for that but we are not really sure how to share data between those two environment. We dont want to use MySql or any other dbms for that not to use ram just for 1 or 2 data.
So the question is; "We want to share 1 or at most 2 data betwe开发者_StackOverflow社区en bash and PHP. How can we do this without third party application or server ?"
Thanks for answers Baris
I assume you have a bash script and you want to run that from PHP and handle the output in some way. PHP has several functions to accomodate that.
The backtick operator example in the PHP docs is:
<?php
$output = `ls -al`;
echo "<pre>$output</pre>";
?>
It's not entirely clear from your question what you're trying to do...but one possible solution would be to write your data to a file or two. Either PHP or your cli environment can read/write the file to get/set the data. Note that if both environments expect to write the data you would need to implement some sort of locking mechanism.
You could also use something like memcached, which would provide you with a memory-based key/value store without the "overhead" of a complete RDBMS solution.
This is really a Stack Exchange question, but PHP runs fine in CLI mode and can do whatever bash would do, without much extra effort. So I would use PHP for the background or CLI process (optionally calling a bash shell script as needed if you want to use PHP as a wrapper only) then use PHP also for the web part. The two parts can share PHP code if required, particularly the part that reads/writes from a shared file.
Locking of this file will be the main challenge as mentioned - creating a lock directory with mkdir (since it's atomic) would be one way, but you'd also need to ensure locks can be cleaned up.
UPDATE: this approach would also work if you want to write to shared RAM instead of a file - you could use memcached or similar from PHP, but there is no way to do that from bash.
精彩评论