Reputation: 427
I would like to read/dump the environment variables of a process (.sh file, command line invocation, etc) that I initiate from inside java.
For example, I have a shell script that runs and sets an environment variable:
#!/bin/bash
export blah='really boring'
I invoke that shell script from a parent java program, but before the shell script exits, I'd like to extract the value of $blah from its environment. This would allow me to communicate information back to the invoking java program besides return code and the stdout/stderr streams.
I suppose I could dump the env to a file and then read the file afterward, but I'd need to specifically modify the script to do that, involve the filesystem, and that seems onerous.
From reading various docs of POSIX exit, there are on_exit hooks I could place on a process, but I was wondering if there is a way to do this using reasonably pure java. Can JNR help?
Any other ideas would be great, the more cross-platform the better, such as wrapping the commands/shell invoked in more UNIXy goodness or other libraries besides the standard groovy/java process initiation library.
Upvotes: 1
Views: 270
Reputation: 533880
You can make your script a little longer to dump the environment variables.
runtime.exec("bash", "-c", ". your-script.sh ; export > export.dump");
Upvotes: 1