An almost forgotten computer administration art: Windows shell scripting - with thanks to Levi Stevens

Summary: computer management requires many skills, including what I would call computer administration skills. Amongst those is shell scripting. It's important to keep an eye open to the most recent solutions, but sometimes older solutions, such as shell scripting, are the right ones for the job at hand.

Yesterday I had a co-worker come to me with a problem related to backups where he wanted to keep 3 versions of the relevant files - no more and no less. He wanted to do it as a batch file, and I couldn't imagine how to do it (I could imagine how to do it with vbscript, but that requires a level of scripting support that wasn't appropriate for the problem). Fortunately, we have another coworker, Levi Stevens, who amongst his many great skills is a Windows shell scripting guru. Within an hour he had the solution that was needed. And even those of us that aren't guru shell scripters can readily maintain it (it's easier to edit than it is to create, as all scripters know, and it's especially easy to edit when the language is simialr to something you know, like regular Windows command statements).

These days we have PowerShell on the Windows platforms to provide a very sophisticated level of shell scripting. Everyone knows how to write basc .BAT files. Those from a *nix background will know Perl. And those with even older backgrounds will be familiar with OpenVMS DCL, or similar languages from the IBM mainframe world. There are lots of options, and each has their virtues, but sometimes the 'close to least common demoninator' is the right solution. Windows shell scripting may be that solution for your problem. There's a learning curve if you're creating the scripts, but it might be much smaller than for other scripting approaches (maybe this blog entry will tell you everything you need to know (no guarantees!))

The script Levi produced itself is wonderous enough, but the real lessons to be learned are:

  • It's best to learn from the experts, which in this case especially includes Tim Hill’s book, "Windows NT Shell Scripting". ISBN 1578700477
  • In many cases, the FOR command is one of your most powerful Windows shell scripting assets. At a command prompt, enter "For /?" to get the details.
  • Similarly, the SET command has a surprising amount of power. Again, at the command prompt, enter "Set /?" to get the details.

 Taking it to the next level, some of Levi's insights are:

  • When it comes to using the FOR command, sometimes you need to filter the output you want to parse. Use the escape character (AKA carrot - ^) on the pipe inside the FOR command. For example, here is a script that gives you the HAL type:

Set REGHAL=HKLM\Hardware\ResourceMap\Hardware Abstraction Layer
For /F "Tokens=5 Delims=\" %%i In ('reg.exe query "%REGHAL%" /s ^| Find /i "Abstraction"') Do Set HAL=%%i
Echo HAL: %HAL%

  • To replace characters from a string, here's another example (with the first line being the string that needs correcting) :

      Set _Tmp=c:/bad/formed(dfd)/url
      Set _Tmp=%_Tmp:/=-%
      Set _Tmp=%_Tmp:\=-%
      Set _Tmp=%_Tmp:(={%
      Set _Tmp=%_Tmp:)=}%
  • Often you will need to increment a number, for example in FOR loops (In this example the "Goto :EOF" will exit the current block when 10 iterations have occurred.):

Set /a LOOP+=1
If %LOOP% GEQ 10 Goto :EOF

p.s. I can't resist - here's one bit of Levi's script that particularly impressed me (the FOR loop):

@ECHO OFF

For /f "Tokens=2,3,4 Delims=/ " %%i In ("%Date%") Do @(
  Set Month=%%i& Set Day=%%j& Set Year=%%k
)

echo %month%
echo %day%
echo %year%

 

Published Thursday, August 23, 2007 8:21 PM by pthomsen
Filed under: ,

Comments

No Comments