The /proc filesystem contains details of running processes. For example on Linux if your PID is 123 then the command line of that process will be found in /proc/123/cmdline
The cmdline is using null-bytes to separate the arguments.
I suspect unpack should be used but I don't know how, my miserable attempts at it using various templates ("x", "z", "C*", "H*", "A*", etc.) just did not work.
This can be done with the command line switches -l
and -0
, or by manually changing $/
.
-l
and -0
are order dependent and can be used multiple times.
Thanks for inspiring me to read perlrun documentation.
# -0 : set input separator to null
# -l012 : chomp input separator (null)
# and set output separator explicitly to newline, octol 012.
# -p : print each line
# -e0 : null program
perl -0 -l012 -pe0 < /proc/$$/environ
.
# -l : chomp input separator (/n) (with -p / -n)
# and set output separator to current input separator (/n)
# -0 : set input separator to null
# -p : print each line
# -e0 : null program
perl -l -0 -pe0 < /proc/$$/environ
.
# partially manual version
# -l : chomp input separator (/n) (with -p / -n)
# and set output separator to current input separator (/n)
# -p : print each line
# -e : set input record separator ($/) explicitly to null
perl -lpe 'INIT{$/="\0"}' < /proc/$$/environ
# DOESN'T WORK:
# -l0 : chomp input separator (/n) (with -p / -n)
# and set output separator to \0
# -e0 : null program
perl -l0 -pe0
.
# DOESN'T WORK:
# -0 : set input separator to null (\0)
# -l : chomp input separator (\0) (with -p / -n)
# and set output separator to current input separator (\0)
# -e0 : null program
perl -0l -pe1
A simple split("\0", $line)
would do the job just fine.
You can set $/
to "\0"
. Example:
perl -ne 'INIT{ $/ = "\0"} chomp; print "$_\n";' < /proc/$$/environ
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With