What I'm trying to do is run the same program multiple times in Cygwin
but with different arguments passed to it.
What I try to do is as follows
./ssltest.exe -3 -p STATIC_PORT STATIC_IP && ./ssltest.exe -p STATIC_PORT STATIC_IP && ./ssltest.exe -1 -p STATIC_PORT STATIC_IP && ./ssltest.exe -2 -p STATIC_PORT STATIC_IP
But in something that would look like this
./bulkssltest.exe -p STATIC_PORT STATIC_IP
And present the same results. As you can see the port and IP remain the same but I run it with -1
, -2
, -3
and with no additional arguments.
The code is way too complicated for me to edit it to achieve the same result but I am pretty sure there is a way to get it to work like this somehow.
Any input would be appreciated.
Put the following into a file, say bulkssltest.sh
:
#!/bin/sh
for opt in -3 "" -1 -2; do
# Intentionally unquoted $opt here, so that the empty opt disappears
# and is not expanded into the empty string (that ssltest doesn't expect)
# You might want to use an absolute path to ssltest.exe here.
./ssltest.exe $opt "$@" || break
done
Make sure the file contains UNIX line endings, or /bin/sh
will complain about $\r: command not found.
and similar things. The trick here is that "$@"
expands to the arguments that were passed to the script and a slightly unusual unquoted use of the loop variable $opt
to make it disappear when it is the empty string. break
exits the loop, and it is invoked here if ssltest
came back with a non-zero exit status (that's what the ||
does; it's the or to &&
's and).
Then:
chmod +x bulkssltest.sh
Then:
./bulkssltest.sh -p STATIC_PORT STATIC_IP
Looping in a batch file is, unfortunately, painful, as is shortcutting, and I have not found a way to expand an empty token to nothing the way I did in the shell script above, so to produce the precise results as the expression you posted, you'll have to repeat yourself. For example, put this into a file bulkssltest.bat
:
@echo off
ssltest -3 %*
IF NOT %ErrorLevel% EQU 0 GOTO end
ssltest %*
IF NOT %ErrorLevel% EQU 0 GOTO end
FOR %%O IN (-2,-1) DO (ssltest %%O %* & IF NOT %ErrorLevel% EQU 0 GOTO end)
:end
and run
bulkssltest -p STATIC_PORT STATIC_IP
The FOR
loop is included here for purposes of illustration; you could, of course, just do the last two calls to ssltest
like the first two. Note that you cannot break this loop into several lines; cmd
will not have it. If the order of execution does not matter, you can fold the call with -3
into the loop and save some lines, but I don't think that is possible for the empty token. Here it is %*
that expands to the arguments that were given to the batch file, %ErrorLevel
is the exit status of the most recently executed program (akin to $?
in shell scripts), and :end
is a jump label.
If you want to do more complicated things with on-board Windows tools, I urge you to look into powershell. It does not get any better with batch files.