Script to get environment variables for a process.
authorIain Patterson <me@iain.cx>
Mon, 27 Jan 2014 14:40:23 +0000 (14:40 +0000)
committerIain Patterson <me@iain.cx>
Mon, 27 Jan 2014 14:51:22 +0000 (14:51 +0000)
Use "readenv <pid>" to get the full environment block for <pid>.
Use "readenv <pid> [<env> [<env> ...]]" to restrict output to the named
variables.

opt/Linux/bin/readenv [new file with mode: 0755]

diff --git a/opt/Linux/bin/readenv b/opt/Linux/bin/readenv
new file mode 100755 (executable)
index 0000000..0547abc
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+pid=$1; shift
+
+if [ -z "$pid" ]; then
+  echo >&2 "Usage: readenv <pid> [<env> [<env> ...]]"
+  exit 1
+fi
+
+xargs -n 1 -0 echo < /proc/$pid/environ | (
+  if [ $# -gt 0 ]; then
+    regex=
+    for i in ${1+"$@"}; do regex="$regex|$i"; done
+    regex=${regex#|}
+    regex=${regex%|}
+    [ -n "$regex" ] && regex="^($regex)="
+    exec grep -E "$regex"
+  else
+    exec cat
+  fi
+)