script to display occupied and non occupied ports in unix

I want to extend this script.

This must also be able to show me that a port is already assigned to a service but not running now

there must be three different messages

port 8949 is open but not listening
port 8959 is open
port 8999 hasn't been assigned to any service.

I know that using telnet we can do but how can I use the telnet command( this must be done using set timeout)

Thanks,
Charan

Your script works just about fine. You might need an if-else construct:

if nc -z $1 $port &>/dev/null; then 
   echo "Port $port is open"
else
  echo "Port $port is not open";
fi

But what does this mean: "hasn't been assigned to any service"? That's meaningless. Either a port is being listened to or it's not.