Reputation: 809
My host is Red Hat Enterprise Linux Server release 5.4
with bash.
When i use this command: netstat -tnpl | grep "tcp" | awk '{print $4}'
,than it output the following:
127.0.0.1:2208
0.0.0.0:871
0.0.0.0:9001
0.0.0.0:3306
0.0.0.0:111
127.0.0.1:631
127.0.0.1:25
127.0.0.1:6010
127.0.0.1:6011
127.0.0.1:2207
:::80
:::22
:::8601
::1:6010
::1:6011
:::443
But when use this: ip_port=`netstat -tnpl | grep "tcp" | awk '{print $4}'` && echo $ip_port
,it becomes the following:
127.0.0.1:2208 0.0.0.0:871 0.0.0.0:9001 0.0.0.0:3306 0.0.0.0:111 127.0.0.1:631 127.0.0.1:25 127.0.0.1:6010 127.0.0.1:6011 127.0.0.1:2207 :::80 :::22 :::8601 ::1:6010 ::1:6011 :::443
It becomes online. How did this happen and what i want is the original format.
Upvotes: 0
Views: 495
Reputation: 274522
You need to enclose your variable in quotes when echoing:
echo "$ip_port"
Also, you don't need to use both grep
and awk
because awk
can grep
. You can simplify your command to:
netstat -tnpl | awk '/tcp/{print $4}'
Upvotes: 2