Port num incrementing

Hello Guyz
These days Iam doing pretty good scripting work that is all because of you fellas.Thank you so much.

As it goes ,here comes my next problem.Iam trying to implement automatic port number incrementing.

For ex:

demo()
{
echo "Enter for default port[56]:
read port
if [[ -z $port ]] #if we didnt enter any thing ,it will take 56
then
$port=56
fi
}
do u want re-run it(y/n):
read ans
if [[ "$ans" = "y" ]]
then
demo
else
exit

-------------------------
This is the code I have so far,what iam trying to do is when ever we re-run the "demo" the port number has to increment by 1.In this case it has to go to 57.
For ex:echo "Enter for default port[57]:

Two things are worrying me now
1)How do i increment to 57 , and make it as default value(if we press enter,it has to take 57)
2)check the port number,if the port number is not available ,hw do we detect it and assign the next available port to default.

These are the things bothering me now.Hope you can help me with these things guyz.

--CoolKid

Try and adapt the following script (demo.sh) :

default_port_file=.default_port
default_port_value=56

get_default_port() {
   if [ -f $default_port_file ]
   then
      default_port=$(<$default_port_file)
   else
      default_port=$default_port_value
   fi
}

set_default_port() {
   if [[ $1 > ${default_port:-$default_port_value} ]]
   then
      echo $1 > $default_port_file
   fi
}

demo()
{
   get_default_port
   echo "Enter port number [$default_port]:"
   read port

   port=${port:-$default_port}
   echo "Port number = $port"
   echo

   set_default_port $((port+1))
}

until [[ "${ans:-y}" != "y" ]]
do
   demo
   echo "do u want re-run it (y/n) [y]:"
   read ans
done
exit

The default port value is memorized is the file .default_port.
When a port number is set, the new default port value is set to this value plus 1 (if greater than actual default value).

$ ls .default_port
/bin/ls: cannot access .default_port: No such file or directory
$ demo.sh
Enter port number [56]:

Port number = 56

do u want re-run it (y/n) [y]:
y
Enter port number [57]:
12
Port number = 12

do u want re-run it (y/n) [y]:
y
Enter port number [57]:
68
Port number = 68

do u want re-run it (y/n) [y]:
y
Enter port number [69]:

Port number = 69

do u want re-run it (y/n) [y]:
n
$ cat .default_port
70
$ demo.sh
Enter port number [70]:

Port number = 70

do u want re-run it (y/n) [y]:
n
$

Jean-Pierre.

Thank You so much Jean for the help.
Iam kinda novice in scripting..so the get_default_port() checks for whether the port is already assigned or not? .Please let me know.

Thanks a lot--CK

how do you start a new thread

The get_default_port function, returns the value of the greatest port used plus 1 (wrtitten in the .default_port file by the set_default_port function).
This function can't check if a port is already assigned.

Jean-Pierre.

Gotcha.....it is working good..Thanks a lot pal.:slight_smile: