Spaces in batch scripts
Since reading The Pragmatic Programmer I’ve become a bit of an automation junkie and writing batch scripts falls right under that category.
Unfortunately, nearly every single time I write one I forget that Windows really hates it when you have spaces in variable assignments, and I forget how to print out a usage message if the right number of parameters are not passed in.
So as much for me as for everyone else, this is how you do it:
12345678910111213141516~ </td> | @ECHO offIF [%1]==[] GOTO usageIF [%2]==[] GOTO usageset VAR1=%1set VAR2=%2rem important client stuffgoto end:usageecho Usage: script.bat var1 var2:endecho Script finished~ </td> </tr> </tbody></table> |
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.