The Windows CMD shell is a primitive language, particularly when compared to unix scripts or even PowerShell. But it’s still the only guaranteed-available common denominator for Windows servers (no, WSH doesn’t count), so I have to target it for setup scripts delivered to customers. When it comes to database activities, I limit this as much as possible by writing portable DB2 CLP SQL files, but that runs out of steam when I need loops, parameters, etc. So I found myself this morning once again bumping up against CMD’s limited vocabulary.
In this case, I needed a multi-line block of SQLs to run in a loop. The begin-end construct for CMD scripts is a pair of parentheses. But my SQLs themselves needed parentheses (as SQLs often do), and I found that Windows doesn’t support nesting them; it failed with a the error message: <statement> was unexpected at this time. No nested parens: just wow. At least the workaround was simple – just move these commands into a sub-script and call it, like so:
for /l %%i in (1,1,9) do ( echo. ==Pass %%i >> %OutputFilename% call ReplicatePass.cmd %%i >>%OutputFilename% if errorlevel 1 goto failure )
I look forward to the day when I can count on PowerShell or a similar robust scripting environment to be available on all Windows platforms I target. Perhaps attrition alone will accomplish this; after all, it has only taken three decades. Until then, I’ll speak slowly to Windows, in words of one syllable.