Computing.Net > Forums > Programming > Scripting Help

Scripting Help

Reply to Message Icon

Original Message
Name: jviola
Date: April 27, 2007 at 07:18:21 Pacific
Subject: Scripting Help
OS: RedHat 4.0
CPU/Ram: Intel , 4Gb
Model/Manufacturer: Dell
Comment:

I have a list of numbers. Below is just a partial list of the numbers. What I would like to do is have a script grab the first 3 numbers (we use these number to assign ports to our users) available and print them in this format: If they are 3 consecutive numbers then use this form 1-3, if not then use this form 1-2,4 or 1,3,5. The end results would look like this: I have th user name part figured out already.

[$username]
ports=$n1-$n2,$n3

Here are some of the numbers:
13
33
34
35
37
38
39
40
92
93
94
95
100



Report Offensive Message For Removal

Response Number 1
Name: Balkrah (by Vaughan Newton)
Date: April 28, 2007 at 00:06:10 Pacific
Subject: Scripting Help
Reply: (edit)

Here is a batch file that stores the result in the ports variable:

@echo off
setlocal EnableDelayedExpansion

set count=1
for /f "usebackq tokens=* delims=" %%a in ("file.txt") do (
if "!count!"=="4" goto :assign
set num!count!=%%a
set /a count+=1
)

:assign
set form=0
set /a num=%num1%+1
if %num%==%num2% set form=2

set /a num=%num2%+1
if %num%==%num3% (
if %form%==2 (set form=1) else (set form=3)
) else if not %form%==2 set form=4

if %form%==1 set ports=%num1%-%num3%
if %form%==2 set ports=%num1%-%num2%,%num3%
if %form%==3 set ports=%num1%,%num2%-%num3%
if %form%==4 set ports=%num1%,%num2%,%num3%

It reads the first three numbers from a file (file.txt). You can specify your own file name.

Hope this helps!



Report Offensive Follow Up For Removal

Response Number 2
Name: tonysathre
Date: April 29, 2007 at 16:10:31 Pacific
Subject: Scripting Help
Reply: (edit)

From looking at his sample code, it looks to me like he needs a Unix shell script, not an NT shell script.

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 3
Name: jviola
Date: April 30, 2007 at 07:39:33 Pacific
Subject: Scripting Help
Reply: (edit)

Thanks Balkrah, However, I need a unix shell script.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Scripting Help

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software