Share data between bash and php - linux

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 between 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.

Related

"Can we read Indexed file using JCL?"

I am looking to read indexed file using JCL is there any possibility of doing like that? Like there is one KSDS file and we have to read that file using indices and we have to print the selected record onto the console using only JCL no usage of COBOL..
I believe the program you are looking to execute with your JCL is IDCAMS, and that you want to use the PRINT FROMKEY() TOKEY() command.
That hyperlink is to the IBM Documentation, a comprehensive set of documentation for z/OS and many of its components. Other IBM products such as Enterprise COBOL, CICS, DB2, and MQ have their own Documentation sites. If you're going to be using an IBM mainframe, it's a good idea to bookmark the sites for the products you use and become familiar with them.
This will not display output on the console, but it will display output on the SYSPRINT DD. I'm not sure if there's a way to display this output on the console (which is where the interface used by mainframe operators), typically that's where messages essential to system health and continued functioning are displayed. If you displayed the output you requested on the console I suspect you'd get a request to not do that right quick.
#NicC is quite correct in saying that the JCL is not doing anything other than requesting the IDCAMS program (in this particular case) be executed. If you're a Linux person, think of it this way:
Suppose you have a shell script...
#! /bin/bash
sort < $1
...would you say the script is doing the work, or the sort program?
JCL has no looping constructs, no way to programmatically alter variables. JCL allows you to request that programs be executed by the operating system and gives you a way to specify their inputs and outputs.

Sensitive Data in Command Line Interfaces

I know it's frowned upon to use passwords in command line interfaces like in this example:
./commandforsomething -u username -p plaintextpassword
My understanding that the reason for that (in unix systems at least) is because it'll be able to be read in the scrollback as well as the .bash_history file (or whatever flavor shell you use).
HOWEVER, I was wondering if it was safe to use that sort of interface with sensitive data programatically while programming things. For example, in perl, you can execute a command using two ``, the exec command, or system command (I'm not 100% sure on the differences between these apart from the return value from the two backticks being the output of the executed command versus the return value... but that's a question for another post I guess).
So, my question is this: Is it safe to do things LIKE
system("command", "userarg", "passwordarg");
as it essentially does the same thing, just without getting posted in scrollback or history? (note that I only use perl as an example - I don't care about the answer specific to perl but instead the generally accepted principle).
It's not only about shell history.
ps shows all arguments passed to the program. The reason why passing arguments like this is bad is that you could potentially see other users' passwords by just looping around and executing ps. The cited code won't change much, as it essentially does the same.
You can try to pass some secrets via environment, since if the user doesn't have an access to the given process, the environment won't be shown. This is better, but is a pretty bad solution too (e.g.: in case program fails and dumps a core, all passwords will get written to disk).
If you use environment variables, use ps -E which will show you environment variables of the process. Use it as a different users than the one executing the program. Basically simulate the "attacker" and see if you can snoop the password. On a properly configured system you shouldn't be able to do it.

Make an file path in Unix filesystem actually point to a program

I'm currently using a piece of software (let's call it ThirdPartyApp) that reads files from a certain directory on my PC. I want to make my own software (call it MyApp) that generates files for ThirdPartyApp. When ThirdPartyApp tries to load /path/to/somefile, instead of somefile getting read from the hard drive, I want MyApp to get called and generate bytes in real time. This is similar to how reading from, say, /dev/urandom doesn't actually load a file called urandom, but instead loads the output of a random generator.
So, my question is, is this even possible to do in userspace? If so, what is this called? I'm not asking for a recommendation of a specific library or anything like that; I just need to know what to google to find info about doing something like this. Oh, and I only care about making this work on Linux, if that's a limiting factor. Thanks!
check out fuse file system : en.wikipedia.org/wiki/Filesystem_in_Userspace – Matt Joyce
Also check out named pipes. Btw, if you control starting this ThirdPartyApp then you can simply run MyApp just before that. – Kenney

Can you load a tree structure in memory with Linux shell?

I want to create an application with a Linux shell script like this — but can it be done?
This application will create a tree containing data. The tree should be loaded in the memory. The tree (loaded in memory) could be readable from any other external Linux script.
Is it possible to do it with a Linux shell?
If yes, how can you do it?
And are there any simple examples for that?
There are a large number of misconceptions on display in the question.
Each process normally has its own memory; there's no trivial way to load 'the tree' into one process's memory and make it available to all other processes. You might devise a system of related programs that know about a shared memory segment (somehow — there's a problem right there) that contains the tree, but that's about it. They'd be special programs, not general shell scripts. That doesn't meet your 'any other external Linux script' requirement.
What you're seeking is simply not available in the Linux shell infrastructure. That answers your first question; the other two are moot given the answer to the first.
There is a related discussion here. They use shared memory device /dev/shm and, ostensibly, it works for multiple users. At least, it's worth a try:
http://www.linuxquestions.org/questions/linux-newbie-8/bash-is-it-possible-to-write-to-memory-rather-than-a-file-671891/
Edit: just tried it with two users on Ubuntu - looks like a normal directory and REALLY WORKS with the right chmod.
See also:
http://www.cyberciti.biz/tips/what-is-devshm-and-its-practical-usage.html
I don't think there is a way to do this as if you want to keep all the requirements of:
Building this as a shell script
In-memory
Usable across terminals / from external scripts
You would have to give up at least one requirement:
Give up shell script req - Build this in C to run as a Linux process. I only understand this up to the point to say that it would be non-trivial
Give up in-memory req - You can serialize the tree and keep the data in a temp file. This works as long as the file is small and performance bottleneck isn't around access to the tree. The good news is you can use the data across terminals / from external scripts
Give up usability from external scripts req - You can technically build a script and run it by sourcing it to add many (read: a mess of) variables representing the tree into your current shell session.
None of these alternatives are great, but if you had to go with one, number 2 is probably the least problematic.

Program embedded inside an ISO

I'm trying to find a good way to embed some application / script inside / beside an ISO file. What I'm trying to achieve is having an cd image which can be used to kick-start its own installation via virtual-manager (virt-install). It's just to simplify the downloads really, so that you don't need to have an installer and an image separately.
The simplest option to do something like that would be to put uu/b64-encoded image after a bash script. The bad side of that is that the file needs to be copied again, its size is bigger than it needs to be and bash likes to reserve a lot of memory for strings that are streamed into some other program.
Another alternative is a Perl script with the binary contents after an __END__. That saves me from encoding the contents and high memory usage, but it doesn't prevent the copy.
Is there any elegant solution then? Are there any script languages that can be embedded inside of another file with no specific header (or at the end)? Or something that can be merged with the ISO format itself?

Resources