The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl + array and incrementing number Optimus_P Shell Programming and Scripting 5 07-18-2008 02:55 AM
Port incrementing using one file coolkid Shell Programming and Scripting 0 06-05-2008 01:42 PM
shell script help: sorting, incrementing environment variables and stuff secoif Shell Programming and Scripting 1 05-08-2008 07:04 AM
New iteration of for-loop without incrementing? jeriryan87 Shell Programming and Scripting 0 07-02-2007 02:13 PM
incrementing a for loop run_time_error Shell Programming and Scripting 2 03-28-2005 02:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-28-2008
coolkid coolkid is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 69
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
  #2 (permalink)  
Old 05-28-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
Try and adapt the following script (demo.sh) :
Code:
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).
Code:
$ 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.
  #3 (permalink)  
Old 05-28-2008
coolkid coolkid is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 69
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
  #4 (permalink)  
Old 05-28-2008
jamesapp jamesapp is offline
Registered User
  
 

Join Date: May 2008
Posts: 12
how do you start a new thread
  #5 (permalink)  
Old 05-28-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
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.
  #6 (permalink)  
Old 05-28-2008
coolkid coolkid is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 69
Smile

Gotcha.....it is working good..Thanks a lot pal.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0