Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


October 2006

10 Keys to Command Shell Scripting

Polish and error-proof your scripts
RSS
Subscribe to Windows IT Pro | See More Performance Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

4. Make Sure the Executables You Want Are Available
If your script relies on programs that aren't a part of the standard OS installation, you must make sure that any such programs are available. You should also document these dependencies in your scripts. One way to ensure executables are available is to put them in the same directory as the script. Then you can run them by using the syntax

"%  ~dp0exename"   ... 

where exename is the name of the executable you want to run. The %~dp0 syntax returns the drive, path, and filename of the current script without quotes. Enclosing the entire string in quotes ensures that the script will still work even if the filename or the directory the script is in contains quotes.

5. Use Double Quotes Correctly
Double quotes (") seem to be a common source of confusion for shell-script writers. Quotes are simply a way of identifying command-line arguments that contain spaces. For example, although it might look correct, the command line

dir C:\Program Files 

gives the Dir command two arguments: C:\Program and Files. To indicate that you intend C:\Program Files to be a single argument (including the space), enclose it in double quotes:

dir "C:\Program Files" 

The quotation marks aren't part of the directory name. They tell the Dir command that the text between them is one argument.

Shell scripts also support command-line arguments. To access a script's command-line arguments, use the notation %n (where n is a number 0 through 9). This notation is called a replaceable parameter (or simply a parameter). For example, the script will replace the %1 parameter with the first command-line argument, %2 with the second argument, and so forth. In addition to %1 through %9, %0 is replaced with the script's name, and %* is replaced with the script's entire command line (not including the script name). I mention command-line arguments here because when Cmd.exe replaces the %1 through %9 parameters with the corresponding command-line arguments, it leaves the double quotes if they're in the argument. This fact leads to a simple observation: A script parameter will always be contained in double quotes if it contains spaces. The following simple rules are based on this observation, and if you follow them carefully, you'll virtually eliminate quoting problems from your shell scripts.

  • Don't put quotes around script parameters (%1 through %9) because they might already contain quotes. If you need to use a parameter with the If command, use characters other than double quotes (e.g., curly braces) to avoid syntax errors. The following script line will work even if the first parameter (%1) contains quotes:
If {%1}=={} Goto :HELP 

The exception to this rule is when you use the syntax %~n to remove a parameter's quotes (which I explain in the next rule).

  • Don't include quotes in the contents of an environment variable. Environment variables can contain spaces without needing quotes. If you need to copy a parameter into a variable, use the syntax %~n (where n is a number 1 through 9), which returns the parameter without quotes. For example, the line
Set DATAFILE=%  ~1 

will copy the script's first parameter ( without quotes) into the variable DATAFILE. The exception to this rule is if you're creating quoted text that will be passed to another command.

  • Remember to put quotes around environment variables if needed. For example, consider the following script lines:
Set TARGET=% ~2
Copy %1 "%TARGET%" 

The first of these lines copies the script's second argument into the TARGET variable, removing the quotes. The second line is syntactically valid because the %1 parameter is already enclosed in quotes if it contains spaces, and the TARGET variable needs quotes because it might contain spaces.

   Previous  1  [2]  3  Next 


Reader Comments
Hi all, note that the URL has changed for the utilities written by the author. The URL is now http://www.westmesatech.com. Regards, Bill

AbqBill March 13, 2008 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 24, 2008

An often irreverent look at some of the week's other news, including a Vista Capable dismissal request, Zune price reductions, Morrow musings, Novell and Microsoft sitting in a tree ... two years later, Yahoo!, IE 6 on Windows Mobile, and so much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...


Related Events Virtualization Management

Virtualization for Mission-Critical BI with SQL Server

Introduction to Identity Lifecycle Manager "2"

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing