Chaining and Grouping Commands
In previous sections, I discussed redirection techniques
that included piping commands. You may have wondered if there were other ways to
execute a series of commands. There are. You can chain commands and execute them
in sequence, and you can execute commands conditionally based on the success or
failure of previous commands. You can also group sets of commands that you want
to execute conditionally.
You’ll learn more about these techniques in the sections that
follow. Before you proceed however, take note of Table 2-3, which provides a quick reference for the
basic syntax to use when chaining or grouping commands. Keep in mind that the
syntax provided is not intended to be all-inclusive. The chaining syntax can be
extended for additional commands to be conditionally executed. The syntax for
grouping may vary, depending on the actual situation.
Symbol
|
Syntax
|
Description
|
---|---|---|
&
|
Command1 & Command2
|
Execute Command1 and then execute Command2.
|
&&
|
Command1 && Command2
|
Execute Command2 if Command1 is completed
successfully.
|
||
|
Command1 || Command2
|
Execute Command2 only when Command1 doesn’t complete
successfully.
|
( )
|
(Command1 & Command2) && (Command3)
|
Use parentheses to group sets of commands for conditional
execution based on success.
|
(Command1 & Command2) || (Command3)
|
Use parentheses to group sets of commands for conditional
execution based on failure.
|
Using Chains of Commands
Sometimes, to be more efficient, you’ll want to execute
commands in a specific sequence. For example, you may want to change to a
particular directory and then obtain a directory listing, sorted by date. Using
chaining, you can perform both tasks by entering this one line of command
text:
cd c:\working\docs & dir /O:d
In scripts, you’ll often need to chain commands such as this to be
certain the commands are carried out exactly as you expect. Still, it makes more
sense to chain commands when the execution of later commands depends upon
whether previous commands succeeded or failed. In this example, a log file is
moved only if it exists:
dir c:\working\logs\current.log && move current.log d:\history\logs
Why would you want to do this? Well, one reason would be so that
an error isn’t generated as output of a script.
You may also want to perform a task only if a preceding command
failed. For example, if you are using a script to distribute files to a group of
workstations, some of which have a C:\Working\Data folder and some of which have
a C:\Data folder, you could copy sets of files to either folder, regardless of
the workstation configuration, using the following commands:
cd C:\working\data || cd C:\data xcopy n:\docs\*.*
Grouping Command Sequences
When you combine multiple commands, you may need a way to
group commands to prevent conflicts or to ensure that an exact order is
followed. You group commands using a set of parentheses. To understand why
grouping may be needed, consider the following example. Here, you want to write
the host name, IP configuration, and network status to a file, so you use this
statement:
hostname & ipconfig & netstat -a > current-config.log
When you examine the log file, however, you find that it contains
only the network status. The reason for this is that the command line executes
the commands in sequence as follows:
-
hostname
-
ipconfig
-
netstat - a > current_config.log
Because the commands are executed in sequence, the system host
name and IP configuration are written to the command line, and only the network
status is written to the log file. To write the output of all the commands to
the file, you would need to group the commands as follows:
(hostname & ipconfig & netstat -a) > current_config.log
Here, the output of all three commands is collected and then
redirected to the log file. You can also use grouping with conditional success
and failure. In the following example, both Command1 and Command2 must succeed
for Command3 to execute:
(cd C:\working\data & xcopy n:\docs\*.*) && (hostname >n:\runninglog.txt)
In the next chapter, you’ll see how command grouping is used with if and if...else
constructs.
No comments:
Post a Comment