Hi there,
I have a little problem with commands. I know this is about shell scripting but i couldn't find a thread for commandlines. So i have this assignment to "display all the lines for tcp- services and udp- services and count the lines" and i honestly don't have an idea how to do that, i know how to count the lines tho XD...if someone could help me i would be grateful. :)

if it is on linux system and you are referring to the active connections to the server use the following:

netstat -a

to list a count of all of the UDP entries use:
netstat -a | grep udp | wc

If you just want TCP info from netstat, you can do netstat -at , or netstat -au for UDP.

You can also get some good information from lsof -i A little late, but I hope this helps!

So the same command works for tcp netstat -a| grep tcp| wc -l ?
or i can use netstat -at| wc -l and netstat -au| wc -l ?
I tried them and got difrent results :D

Well both of those methods are imperfect for counting connections, but you're probably getting a more accurate result with grep in this case, because it won't count the first two lines (if your output is like mine)

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State

Also consider that the result may actually be different from one moment to the next.

Here's perhaps a more accurate way to do it:

echo "TCP Connections: $(netstat -ant|awk 'END {print NR-2}')"
echo "UDP Connections: $(netstat -anu|awk 'END {print NR-2}')"

OR I like to do things where we're getting multiple numbers from the same output, in more of a snapshot form since the numbers CAN change from one moment to the next. Something like this may be more appropriate, depending on the ultimate goal of your script:

NETSTAT=$(netstat -an)
echo "TCP: $(echo "$NETSTAT"|grep -c ^tcp)"
echo "UDP: $(echo "$NETSTAT"|grep -c ^udp)"
commented: The awk and grep examples were great and I had never used the output to a variable like that. Well written. +2

yeah, i thought they may differ because of the two first lines :D

Thanks for the help:)