Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


June 28, 2001

Shell Scripting 101, Lesson 7

RSS
Subscribe to Windows IT Pro | See More Windows NT 4.0 Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

The & Symbol
If you want to execute commands consecutively (e.g., Command A, then Command B), you place the & symbol between the commands:

Command A & Command B

You’re not limited to joining only two commands. You can join any number of commands. For example, you can use the & symbol to place the three commands

Echo Fred
Echo Wilma
Echo Barney

on one line

Echo Fred & Echo Wilma & Echo Barney

What are the benefits of putting commands together on the same line? Joining commands saves space in scripts. For example, suppose you want to test to see whether the C:\boot.ini file exists. If the file exists, you want to display the names Fred, Wilma, and Barney onscreen. If the file doesn't exist, you want to display the name Dino onscreen. As Listing 1 shows, if you don’t use the & symbol to join the commands, you need eight lines of code to accomplish this task. If you use the & symbol, you accomplish the same task with only three lines of code, as Listing 2 shows. In addition to the space advantage, the code in Listing 2 is much easier to read. You can mentally picture that the three Echo commands will execute if C:\boot.ini exists. (If you’re unfamiliar with the GoTo and If Exist commands in Listing 1 and Listing 2, see "Shell Scripting 101, Lesson 5" and "Shell Scripting 101, Lesson 6," respectively.)

The && Symbol
You use the && symbol to specify that you want to run Command A, and if Command A is successful, you want run Command B. In other words, Command B runs only if Command A succeeds. Although the && symbol’s syntax

Command A && Command B

looks similar to the & symbol’s syntax, the two symbols are quite different. The best way to see the difference is to use the & and && symbols in the same code. If you run the code

Dir /ad M:\ & Echo An M drive exists 

the Echo command displays the message An M drive exists (i.e., Command B) whether or not the Dir command (i.e., Command A) executes successfully. If you run the same code but this time with the && symbol

Dir /ad M:\ && Echo An M drive exists

the Echo command displays the message An M drive exists only if the Dir command executes successfully (i.e., you have an M drive). If you don't have an M drive, the Echo command doesn’t execute and you don’t receive the message.

The || Symbol
The syntax for the || symbol is

Command A || Command B

The || symbol is similar to the && symbol in that Command B’s execution hinges on Command A’s results. However, with the || symbol, Command B’s execution hinges on Command A’s failure rather than its success. In other words, Command B runs only if Command A fails. For example, if you run the code

Dir /ad M:\ || Echo An M drive doesn’t exist.

the command processor displays the message An M drive doesn’t exist only if the Dir command fails.

Homework Assignments

  1. Explore what happens when you add the /v switch to the Find command. Try running the command
  2. Dir /ad C:\ | Find /i /v "winnt"

    and compare the results with those of the Find command you ran previously.

  3. Find all the folders in the \winnt directory that don’t contain the string "drivers" in the folder name. Here’s a hint: Use the | symbol to chain together several Find commands.

End of Article

   Previous  1  [2]  Next  


Reader Comments
<P>When I run <BR>
<BR>
Dir /ad C:\ | Find /i "winnt" <BR>
<BR>
it does NOT just give me the search directory (i.e., /winnt) but also all the contents under it. Is there are way to just get the /winnt directory as the result vs. all the underlying contents also?</P>

Naufal January 20, 2004


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Command Prompt Tricks

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

Understanding File-Size Limits on NTFS and FAT

A general confusion about files sizes on FAT seems to stem from FAT32's file-size limit of 4GB and partition-size limit of 2TB. ...


Related Articles Shell Scripting 101, Lesson 10

Shell Scripting 101, Lesson 9

Shell Scripting 101, Lesson 8

Shell Scripting 101, Lesson 6

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events WinConnections and Microsoft® Exchange Connections

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 Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


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 DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement