i got a question about bash and awk, so, i'm going to filter the processes list from ps aux
then i did filtering by grep
, then i filter it again using awk
to display only the pid and the path of the process, then save it to the corresponding ${pid}
and ${path}
field. the question is, when i done filtering the results using awk, i'm going to save those results on ${pid}
for pid numbers and ${path}
for process's path, but i got no idea at all on doing that thing. if anyone here have a solution it would be very appreciated.
Thanks
EDIT: here is the code
ps aux | grep 'firefox' | awk '{print $2 " " $11}'
then i don't know what to do to save the $2
content to ${pid}
and $11
to ${path}
and save those fields to the txt file again...
应该这样做:
read pid path <<< $(ps aux | grep 'firefox' | awk '{print $2 " " $11}')
If all else fail remember that you have the disk that you can use. Pipe the result to a file and then process the file using awk
or cut
two times to assign the fields to the corresponding variables.
For example:
$result > tmp
pid=`cat tmp | cut -f 1`
path=`cat tmp | cut -f 2`
rm tmp
use set :
out=$(ps aux | grep 'nginx' | awk '{print $2 " " $11}' )
set $out
pid=$1
path=$2
echo $pid
echo $path
awk_output=($(ps aux | awk '/firefox/{print $2, $11}'))
pid="${awk_output[0]}"
path="${awk_output[1]}"
If your paths can contain spaces you'll need a different solution involving setting IFS.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.