Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

Linux bash取值问题

命令:cat /proc/10725/environ
结果:

XDG_SESSION_ID=355SELINUX_ROLE_REQUESTED=SHELL=/bin/bashHADOOP_HOME=/usr/hadoop/hadoop-3.0.0SSH_CLIENT=127.0.0.1 59240 22HDFS_NFS3_SECURE_EXTRA_OPTS=-jvm serverSELINUX_USE_CURRENT_RANGE=HDFS_DATANODE_SECURE_EXTRA_OPTS=-jvm serverHDFS_DATANODE_OPTS=-Dhadoop.security.logger=ERROR,RFASHDFS_PORTMAP_OPTS=-Xmx512mUSER=rootMALLOC_ARENA_MAX=4HDFS_SECONDARYNAMENODE_OPTS=-Dhadoop.security.logger=INFO,RFASPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/binMAIL=/var/mail/rootHADOOP_ENV_PROCESSED=trueHADOOP_HDFS_HOME=/usr/hadoop/hadoop-3.0.0HADOOP_COMMON_HOME=/usr/hadoop/hadoop-3.0.0PWD=/rootHADOOP_YARN_HOME=/usr/hadoop/hadoop-3.0.0JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64/jre/HADOOP_OS_TYPE=LinuxHADOOP_CONF_DIR=/usr/hadoop/hadoop-3.0.0/etc/hadoopLANG=zh_CN.UTF-8HADOOP_OPTS=-Djava.net.preferIPv4Stack=true -Dhdfs.audit.logger=INFO,NullAppender -Dhadoop.security.logger=INFO,RFAS -Dyarn.log.dir=/usr/hadoop/hadoop-3.0.0/logs -Dyarn.log.file=hadoop-root-namenode-localhost.localdomain.log -Dyarn.home.dir=/usr/hadoop/hadoop-3.0.0 -Dyarn.root.logger=INFO,console -Djava.library.path=/usr/hadoop/hadoop-3.0.0/lib/native -Dhadoop.log.dir=/usr/hadoop/hadoop-3.0.0/logs -Dhadoop.log.file=hadoop-root-namenode-localhost.localdomain.log -D**hadoop.home.dir=/usr/hadoop/hadoop-3.0.0** -Dhadoop.id.str=root -Dhadoop.root.logger=INFO,RFA -Dhadoop.policy.file=hadoop-policy.xmlSELINUX_LEVEL_REQUESTED=HOME=/rootSHLVL=1HADOOP_MAPRED_HOME=/usr/hadoop/hadoop-3.0.0LOGNAME=rootCLASSPATH=/usr/hadoop/hadoop-3.0.0/etc/hadoop:/usr/hadoop/hadoop-3.0.0/share/hadoop/common/lib/*:/usr/hadoop/hadoop-3.0.0/share/hadoop/common/*:/usr/hadoop/hadoop-3.0.0/share/hadoop/hdfs:/usr/hadoop/hadoop-3.0.0/share/hadoop/hdfs/lib/*:/usr/hadoop/hadoop-3.0.0/share/hadoop/hdfs/*:/usr/hadoop/hadoop-3.0.0/share/hadoop/mapreduce/lib/*:/usr/hadoop/hadoop-3.0.0/share/hadoop/mapreduce/*:/usr/hadoop/hadoop-3.0.0/share/hadoop/yarn:/usr/hadoop/hadoop-3.0.0/share/hadoop/yarn/lib/*:/usr/hadoop/hadoop-3.0.0/share/hadoop/yarn/*SSH_CONNECTION=127.0.0.1 59240 127.0.0.1 22HDFS_NAMENODE_OPTS=-Dhadoop.security.logger=INFO,RFASLESSOPEN=||/usr/bin/lesspipe.sh %sXDG_RUNTIME_DIR=/run/user/0HDFS_AUDIT_LOGGER=INFO,NullAppender

我现在想取里头:hadoop.home.dir=/usr/hadoop/hadoop-3.0.0 这个值,shell: cat /proc/10725/environ | 后面命令应该怎么筛选? ??


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

使用sed命令替换, 只保留正则hadoop.home.dir=[^* ]+匹配到的这部分

cat /proc/10725/environ | sed -E 's/.+(hadoop.home.dir=[^* ]+).+/1/'

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...