【发布时间】:2015-07-20 01:21:30
【问题描述】:
#! /bin/bash
a=`netstat -plant | grep -i listen`
#to get ip and port
b=`echo $a | awk {'print $4}'`
#to get the process id
c=`echo $a | awk {'print $7}' | awk -F '/' {'print $1'}`
set -- $c
#to get the details of process
g=`ps aux | grep $1`
m=`echo $g | awk {'print $2}'`
n=`echo $g | awk {'print $9}'`
o=`echo $g | awk {'print $11}'`
echo The process with PID $m invoked by command "$o", is listening at IP and Port : $b . The process has been running since $n
我试图制作一个脚本来以一种简单的语言显示 PID、IP、端口、运行以来的详细信息以及所有 tcp 侦听进程的命令。我制作的脚本只给出了 1 个进程的详细信息
【问题讨论】:
-
使用括号比使用旧的反引号更好,例如:
m=$(echo $g | awk {'print $2}'。双引号变量也很好,所以:m=$(echo "$g" | awk {'print $2}'或像这样:m=$(awk {'print $2}' <<< "$g"
标签: shell awk sed scripting grep