You can easily join commands with command symbols. You can use four symbols: pipe (|), ampersand (&), double ampersand (&&), and double pipe (||).
The | Symbol
The | symbol pipes the output from one command (Command A) to another command (Command B). You place the | symbol between the commands:
Command A | Command B
You often have to filter a command’s output before using it. Thus, you’ll probably use the | symbol often to pipe output to the Find command. The Find command searches a command’s output (or text from an input file) for the string you specify. The Find command displays any line that contains the string and discards all the other lines.
For example, suppose you want to know when the C:\winnt directory was created. As I discussed in "Shell Scripting 101, Lesson 2," the Dir command lists information, including creation date and time, about the subdirectories and files in the specified drive or directory. In this case, you use the command
Dir /ad C:\
The /a switch followed by the letter d specifies that you want to list only directories on the C drive.
You now have the information you want, so you just need to pipe it to the Find command to filter out the information you don’t want. As the command
Dir /ad C:\ | Find /i "winnt"
shows, you use the | symbol to pipe the Dir command’s output to the Find command and specify "winnt" as the string for which to search. You need to enclose the string in quotes. The /i switch after Find makes the search case insensitive. As Figure 1 shows, when you run this command, you receive just the line that contains information about the \winnt directory.
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
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.
- Explore what happens when you add the /v switch to the Find command. Try running the command
- 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.
Dir /ad C:\ | Find /i /v "winnt"
and compare the results with those of the Find command you ran previously.