Subscribe to Windows IT Pro
June 28, 2001 12:00 AM

Shell Scripting 101, Lesson 7

Windows IT Pro
InstantDoc ID #21630
Rating: (0)
Downloads
21630.zip

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.

Related Content:

ARTICLE TOOLS

Comments
  • Naufal
    8 years ago
    Jan 20, 2004

    When I run



    Dir /ad C:\\ | Find /i "winnt"



    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?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.