![](https://csdnimg.cn/release/download_crawler_static/4091325/bg14.jpg)
POWERSHELL AND UNIX SHELLS 15
I probably would have been one of them.) The point here isn’t to say
that PowerShell is better than the
UNIX shells. Rather, it’s that they take
different approaches to solving similar problems on different systems. As
a result, this becomes something of an apples and oranges discussion.
Although inspired by the
UNIX shells, PowerShell is different from them. The most
obvious difference is that PowerShell uses objects as the basic model of interaction
instead of strings. Second, the list of built-in commands is both larger and end-user
extensible, by which I mean that users can create extensions that are loaded into the
PowerShell process.
Because the same extension mechanism is used by all commands, there is no dif-
ference between the built-in commands and user-created extension cmdlets. This
model is necessitated by and a consequence of the decision to use objects. The out-of-
process extension model used by traditional shells is impractical for an object-based
shell. Even using
XML as an intermediate representation is impractical due to the cost
of serializing and deserializing each object.
NOTE There are UNIX shells that allow end-user extensions. For exam-
ple, the Desktop Korn Shell (dtksh), part of the Common Desktop
Environment
(CDE), allows for in-process end-user extensions. This lets
it call into various
GUI libraries including the Tk part of Tcl/Tk.
Instead of doing a feature-by-feature comparison of PowerShell and the
UNIX shells,
the approach in this section is to work through a set of illustrative examples showing
how a problem can be solved in each environment. Because the examples are
designed to illustrate features in PowerShell, the corresponding
UNIX examples are
non-optimal.
A.2.1 Stopping all processes
To stop all processes that begin with the letter p on a
UNIX system, you type the fol-
lowing shell command line:
$ ps -e | grep " p" | awk '{ print $1 }' | xargs kill
The
ps
command retrieves a list of processes and sends the output text to
grep
. The
grep
command searches the string for processes whose names begin with p. The out-
put of
grep
is, in turn, sent to the
awk
command, which selects the first column in
the input text (which is the process
ID) and then passes those to the
xargs
com-
mand. The
xargs
command executes the
kill
command for each process it receives
as input. Beyond the complexity of the number of stages that need to be executed,
this command is also fragile. The problem is that the
ps
command behaves differ-
ently on different systems (and sometimes on different versions of the same system).
For example, the
-e
flag on
ps
may not be present; or if the processed command isn’t
in column 1 of the output, this command-line procedure will fail.