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.8k views
in Technique[技术] by (71.8m points)

linux bash多行取值

#! /bin/bash
# process-monitor.sh

process=$1
pid=$(ps x | grep "hadoop" | grep '/bin/java' | grep -v grep | awk '{print $1}')
echo $pid`

上面是脚本,执行后返回值有多个pid:

10725 10855 11034 11289 11425

比如我想取第一个10725,该怎么写,感谢??。


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

1 Answer

0 votes
by (71.8m points)
pid=$(ps x | grep "hadoop" | grep '/bin/java' | grep -m1 -v grep | awk '{print $1}')

还有一种更优雅的解决方案可以避免grep -v grep这种方式:

pid=$(ps x | grep '[h]adoop' | grep -m1 '/bin/java' | awk '{print $1}')

See: https://unix.stackexchange.co...


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

...