Wednesday, December 12, 2007

more on migrating from unix to windows

Although the Windows XP command shell and standard executables aren't as powerful as standard unix utilities, there are some features that provide similar functionality.  Some of the ones I've found helpful, along with their unix equivalents, are listed below.

 


  • fgrep -> find.  Basic substring search in multiple files.  The text string has to be quoted.  See find /? for usage.
  • grep -> findstr.  Supports basic regular expressions.  If the pattern contains spaces use /C, e.g., /C:"^pattern with spaces$" .  See findstr /? for usage.
  • find -> haven't found a command line equivalent with as much power but some of the functionality can be accomplished with dir, FOR and Windows Explorer's search (ok that's not a command line equivalent but it's a way to find files by pattern).

    • dir has several options, like /S for recurse, /A:d for directories, etc...  see dir /? for usage.  dir takes a filename glob.  For more powerful searching pipe the output to find or findstr ala dir /a:d | find "pp" to find all directories in the current directory that contain pp in their name.
    • FOR has several options that approximate find.  e.g., /R for recurse.  See FOR /? for usage.

  • loops (e.g., for in bash or foreach tcsh) -> FOR in the xp command shell with a few caveats.  Variables are denoted with %s instead of $.  The % is needed for the formal parameter.  So bash's for i in a b c ; do echo $i ; done becomes for %i in (a b c) DO echo %i in the xp command shell.

    • You can execute a multi-line command by placing it within parentheses.
    • By default, all variables except the FOR variable are evaluated once.  So if the body of your FOR loop changes the value of a variable the variable will only take the first value.  To change this, the command shell must be run with the /V:ON option.  Which changes the %s to !s vis a vis variable expansion.  See cmd /? for usage.

  • exit codes -> you can test for ERRORLEVEL in IF statements.  See IF /? for usage.  Not every executable sets a non-zero error code to indicate an error but the built-in commands seem to.
  • `cmd` parsing -> FOR /f.  To get the year and month of each line of the dir statement you would use for /f "usebackq tokens=1,2,* delims=/" %a in (`dir`) do echo a=%a b=%b

    • See for /? for usage.
    • If your FOR variable is j and you specify tokens then additional variables, starting at j, will be allocated (e.g., %j, %k, %l, etc....)

  • shift -> shift.  Identical!  Of course, you're still using %0, %1, etc... instead of $0, $1, ...  shift is a handy way to get around the 10 positional parameters limit.
  • variables -> set varname=value where value.  Values needn't be quoted.  To evaluate varname use %varname%.  It will only take the first value that is assigned to it unless you turn on delayed expansion via cmd /V:on in which case use !varname!
  • arithmetic -> bash's $((expr)) becomes set /a expr where expr includes the variable being assigned to and the operation being performed.  e.g.,

#!/bin/bash

x=$((1 + 1))

echo $x

becomes

set /a x=1+1

echo %x%

 

No comments :

Post a Comment