pa4*_*080 25 command-line bash proc
我试图以/proc/*PID*/environ更易读的格式获取任何文件的内容。我可以按照下面显示的方式做到这一点,但我确信这不是正确的方式。
$ cat "/proc/$(pgrep gnome-session -n -U $UID)/environ"
USER=spasTEXTDOMAIN=im-configXDG_SEAT=seat0XDG_SESSION_TYPE=waylandSHLVL=1QT4_IM_MODULE=ximHOME=/home/spasDESKTOP_SESSION=ubuntuGNOME_SHELL_SESSION_MODE=ubuntuDBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/busIM_CONFIG_PHASE=2LOGNAME=spasGTK_IM_MODULE=ibusJOURNAL_STREAM=9:147845_=/usr/bin/gnome-sessionUSERNAME=spasXDG_SESSION_ID=70PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/binXDG_RUNTIME_DIR=/run/user/1000LANG=en_US.UTF-8XDG_CURRENT_DESKTOP=ubuntu:GNOMEXDG_SESSION_DESKTOP=ubuntuXMODIFIERS=@im=ibusSHELL=/bin/bashGDMSESSION=ubuntuTEXTDOMAINDIR=/usr/share/locale/XDG_VTNR=2QT_IM_MODULE=ximPWD=/home/spasCLUTTER_IM_MODULE=ximXDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share:/usr/share:/var/lib/snapd/desktopXDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
Run Code Online (Sandbox Code Playgroud)
$ cat -e "/proc/$(pgrep gnome-session -n -U $UID)/environ"
USER=spas^@TEXTDOMAIN=im-config^@XDG_SEAT=seat0^@XDG_SESSION_TYPE=wayland^@SHLVL=1^@QT4_IM_MODULE=xim^@HOME=/home/spas^@DESKTOP_SESSION=ubuntu^@GNOME_SHELL_SESSION_MODE=ubuntu^@DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus^@IM_CONFIG_PHASE=2^@LOGNAME=spas^@GTK_IM_MODULE=ibus^@JOURNAL_STREAM=9:147845^@_=/usr/bin/gnome-session^@USERNAME=spas^@XDG_SESSION_ID=70^@PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin^@XDG_RUNTIME_DIR=/run/user/1000^@LANG=en_US.UTF-8^@XDG_CURRENT_DESKTOP=ubuntu:GNOME^@XDG_SESSION_DESKTOP=ubuntu^@XMODIFIERS=@im=ibus^@SHELL=/bin/bash^@GDMSESSION=ubuntu^@TEXTDOMAINDIR=/usr/share/locale/^@XDG_VTNR=2^@QT_IM_MODULE=xim^@PWD=/home/spas^@CLUTTER_IM_MODULE=xim^@XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share:/usr/share:/var/lib/snapd/desktop^@XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg^@
Run Code Online (Sandbox Code Playgroud)
$ cat -e "/proc/$(pgrep gnome-session -n -U $UID)/environ" | sed 's/\^@/\n/g'
USER=spas
TEXTDOMAIN=im-config
XDG_SEAT=seat0
XDG_SESSION_TYPE=wayland
...
Run Code Online (Sandbox Code Playgroud)
也许我必须为 分配一个特定的值$IFS,但它是什么?达到上述结果的正确方法是什么?
mur*_*uru 38
条目由空字符分隔,请参阅man 5 proc:
/proc/[pid]/environ
This file contains the environment for the process. The entries
are separated by null bytes ('\0'), and there may be a null byte
at the end.
Run Code Online (Sandbox Code Playgroud)
所以一个简单的方法是应用xargs -0 -L1它:
$ xargs -0 -L1 -a /proc/self/environ
LC_CTYPE=UTF-8
USER=muru
LOGNAME=muru
HOME=/home/muru
MAIL=/var/mail/muru
SHELL=/bin/zsh
...
Run Code Online (Sandbox Code Playgroud)
-0 - 读取空分隔行,-L1 - 每次执行命令读取一行-a file 从中读取行 filexargs只需打印该行。各种GNU命令有选择与空分隔的数据的工作:-z对sed,sort,uniq,grep等,并为文件名,-print0与find和-Z使用grep。
或者,您可以使用普通的旧 bash:
while IFS= read -d '' -r line
do
printf "%q\n" "$line"
done < /proc/.../environ
Run Code Online (Sandbox Code Playgroud)
-d ''告诉read读取直到空字节,IFS=并-r防止字段拆分和反斜杠转义,以便按原样读取数据,%q将在输出中引用特殊字符。
既然你确实使用了sed,你可以这样做:
sed -z 's/$/\n/' /proc/.../environ
Run Code Online (Sandbox Code Playgroud)
它只是在每个空分隔行的末尾添加一个换行符。
Xen*_*050 17
你可以使用cut:
cut -d '' -f1- --output-delimiter=$'\n' /proc/$pid/environ
Run Code Online (Sandbox Code Playgroud)
使用 null 作为分隔符,并输出换行符,可选择仅选择某些字段/行。
或者过滤通过tr,将空值转换为换行符:
tr '\0' '\n' </proc/$pid/environ
Run Code Online (Sandbox Code Playgroud)或者只是坚持你的sed版本......
Yar*_*ron 13
您可以strings按如下方式使用:
strings /proc/$(pgrep gnome-session -n -U $UID)/environ
Run Code Online (Sandbox Code Playgroud)
输出示例:
$ strings /proc/$(pgrep gnome-session -n -U $UID)/environ
GNOME_KEYRING_PID=
LANGUAGE=en_US
J2SDKDIR=/usr/lib/jvm/java-8-oracle
LC_TIME=en_US.UTF-8
XDG_SEAT=seat0
XDG_SESSION_TYPE=x11
COMPIZ_CONFIG_PROFILE=ubuntu
SESSION=ubuntu
Run Code Online (Sandbox Code Playgroud)
姓名
Run Code Online (Sandbox Code Playgroud)strings - print the strings of printable characters in files.描述
Run Code Online (Sandbox Code Playgroud)For each file given, GNU strings prints the printable character sequences that are at least 4 characters long (or the number given with the options below) and are followed by an unprintable character. By default, it only prints the strings from the initialized and loaded sections of object files; for other types of files, it prints the strings from the whole file. strings is mainly useful for determining the contents of non-text files.