String concatenation and arithmetic
Posted: 05 Apr 2011 11:03
Hello,
For starters, I'm somewhat of a newbie when it comes to DOS Batch (CMD) language. I've started to write a small batch program which will be used to build a list of hosts, and a total number of CPUs requested (I hope to store these two items in separate variables).
The batch program can expect to always find a variable in the environment where it is executed of the form:
HOSTLIST=<nodename0> <number of CPUs><nodename1> <number of CPUs> ... <nodenameN><number of CPUs>
For example:
HOSTLIST=host000 20 host001 20 host002 20
What I would like to extract from this list is:
HOSTS=host000 host001 host002
CPUS=60 (this is the sum of 20+20+20)
I've written a few lines to calculate the CPUS value, but I quickly realized that this was not working properly (when number of CPUs > 9):
@ECHO OFF
set HOSTLIST=host000 2 host001 3
SET CPUS=0
SET HOSTS=
FOR %%j IN (%HOSTLIST%) DO (
FOR %%f IN (0 1 2 3 4 5 6 7 8 9) DO (
IF %%j==%%f SET /A CPUS=CPUS+%%j
)
)
echo CPUS=%CPUS%
Note that above I did not even start to explore building the HOSTLIST. So what I need to understand is how I can step through the the HOSTLIST and determine if a value is numeric or not. If yes, then it needs to be added to a running total, if not, it needs to be added to a list of hosts (concatenated).
I've looked at the article: viewtopic.php?f=3&t=193 but it does not appear to work as expected for this case.
Any pointers would be greatly appreciated.
For starters, I'm somewhat of a newbie when it comes to DOS Batch (CMD) language. I've started to write a small batch program which will be used to build a list of hosts, and a total number of CPUs requested (I hope to store these two items in separate variables).
The batch program can expect to always find a variable in the environment where it is executed of the form:
HOSTLIST=<nodename0> <number of CPUs><nodename1> <number of CPUs> ... <nodenameN><number of CPUs>
For example:
HOSTLIST=host000 20 host001 20 host002 20
What I would like to extract from this list is:
HOSTS=host000 host001 host002
CPUS=60 (this is the sum of 20+20+20)
I've written a few lines to calculate the CPUS value, but I quickly realized that this was not working properly (when number of CPUs > 9):
@ECHO OFF
set HOSTLIST=host000 2 host001 3
SET CPUS=0
SET HOSTS=
FOR %%j IN (%HOSTLIST%) DO (
FOR %%f IN (0 1 2 3 4 5 6 7 8 9) DO (
IF %%j==%%f SET /A CPUS=CPUS+%%j
)
)
echo CPUS=%CPUS%
Note that above I did not even start to explore building the HOSTLIST. So what I need to understand is how I can step through the the HOSTLIST and determine if a value is numeric or not. If yes, then it needs to be added to a running total, if not, it needs to be added to a list of hosts (concatenated).
I've looked at the article: viewtopic.php?f=3&t=193 but it does not appear to work as expected for this case.
Any pointers would be greatly appreciated.