Skip to content

Instantly share code, notes, and snippets.

@tataranovich
Last active August 23, 2020 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tataranovich/3a8f1f4978ea5ab7784af092de8a58fa to your computer and use it in GitHub Desktop.
Save tataranovich/3a8f1f4978ea5ab7784af092de8a58fa to your computer and use it in GitHub Desktop.
Grab environment
#!/bin/sh
XPROC="dbus-launch"
XPID=""
TARGET="$1"
if [ ! -z "$TARGET" ]; then
# Target is specified, check is it PID or process name
if echo "$TARGET" | grep -Eq '^[0-9]+$'; then
# Target is number, use it as PID
XPID="$TARGET"
XPROC=""
else
# Target is not number, use it as process name
XPROC="$TARGET"
fi
fi
if [ -z "$XPID" ]; then
# Find PID for process name started by current user
XPID=$(pgrep -u "$(whoami)" "$XPROC" | head -n 1)
fi
if [ ! -r "/proc/$XPID/environ" ]; then
echo "Failed to grab environment from $XPROC($XPID)" >&2
exit 1
else
xargs -r0 -n1 echo export < "/proc/$XPID/environ"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment