Most shells (such as Windows CMD.EXE and the UNIX shells SH, KSH, CSH, and BASH) operate by executing a command or utility in a new process, and presenting the results (or errors) to the user as text. Text-based processing is the way in which system interaction is done with these shells. Over the years, a large number of text processing utilities—such as sed, AWK, and PERL—have evolved to support this interaction. The heritage of this operational process is very rich.
MSH is very different from these traditional shells. First, this shell does not use text as the basis for interaction with the system, but uses an object model based on the .NET platform. Second, the list of built-in commands is much larger; this is done to ensure that the interaction with the object model is accomplished with the highest regard to integrity with respect to interacting with the system. Third, the shell provides consistency with regard to interacting with built-in commands through the use of a single parser, rather than relying on each command to create its own parser for parameters.
This document constrasts the Korn Shell (KSH) and MSH by providing a number of examples of each.
To stop all processes that begin with the letter “p” on a UNIX system, an administrator would have to type the following shell command line:
The ps command retrieves list of processes and directs (|) the grep command to determine which process begins with “p”; which in turn directs (|) the awk command to select the 1st column (which is in the process id) and then passes (|) those to the xargs command which then executes the kill command for each process. The fragility of this command line may not be evident, but if the ps command behaves differently on different systems, where the “-e” flag may not be present, or if the processed command is not in column 1 of the output, this command-line procedure will fail.
Thus, the command line may now be expressed as “get the processes whose name starts with “p” and stop them”. The get-process Cmdlet takes an argument that matches the process name; the objects returned by get-process are passed directly to the stop-process Cmdlet that acts on those objects by stopping them.
The second, more convoluted example, which stops processes that use more than 10 MB of memory becomes quite simple.
Another, even more complex example, such as “find the processes that use more than 10 MB of memory and kill them” can lead to an equally failed outcome:
The success of this command line relies on the user knowing that the ps -el command will return the size of the process in kilobytes (kb) in column 6 and that the PID of the process is in column 3. It is still required that the first row is removed.
Comparing Example 1 using a standard shell to Example 1a using MSH, we can see that the commands act against objects rather than against text.
There is no issue about determining the column that contains the size of the process, or which column contains the ProcessID. The memory size may be referred to logically, by its name. The where Cmdlet can inspect the incoming object directly and refer to its properties. The comparison of the value for that property is direct and understandable.
For example, if you wanted to calculate the number of bytes in the files in a directory, you would iterate over the files, getting the length and adding to a variable, and then print the variable:
This example uses the set shell command that creates numbered variables for each white space separated element in the line rather than the awk command as in the examples above. If the awk command were used, it would be possible to reduce the steps to the following:
This reduces the complexity, but requires specific knowledge of a new language, the language that is associated with the awk command.
The MSH loop is similar; each file in the directory is needed, but it is far simpler as the information about the file is already retrieved:
The measure-object Cmdlet interacts with objects and if it is provided with a property from the object, it will sum the values of that property. Because the property length represents the length of the file, the measure-object Cmdlet is able to act directly on the object by referring to the property name rather than “knowing” that the length of the file is in column 3 or column 5.
Many objects provided by the system are not static, but dynamic. This means that after an object is acquired, it is not necessary to acquire the object at a later time. The data in the object is updated as the conditions of the system change. Also, changes to these objects are reflected immediately in the system.
As an example, suppose one wanted to collect the amount of processor time that a process used over time. In the traditional UNIX model, the ps command would need to be run iteratively and the appropriate column in the output would need to be found and then the subtraction would need to be done. With a shell that is able to access the process object of the system, it is possible to acquire the process object once, and since this object is continually updated by the system; all that is necessary is to refer to the property. The following examples illustrate the differences, where the memory size of an application is checked in ten second intervals and the differences are output:
It is even more difficult to determine whether a specific process is no longer running. In this case, the UNIX user must collect the list of processes and compare them to another list.
As is seen in this example, the MSH user need only collect the object and then subsequently refer to that object.
For example, suppose the user wanted to determine which processes were compiled as PreRelease code, such as when applications have been compiled in such a way to mark them as “PreRelease“.
This information is not kept in the standard UNIX executable. To determine this information, one would need to have a set of specialized utilities to add this information to the binary and then another set of utilities to collect this information. These utilities do not exist; it is not possible to accomplish this task.
In this example, a cascade of properties is done. The appropriate property from the process object (MainModule) is inspected, the property “FileVersionInfo” is referenced (a property of MainModule) and the value of the property “IsPreRelease” is used to filter the results. If IsPreRelease is true, the objects that are output by the get-process Cmdlet are output.
Each object may or may not provide methods; MSH provides commands to aid the discovery of methods that are available for a specific object via the get-member Cmdlet. For example, the string object has a large number of methods:
As can be seen, 46 different methods are available to the string object all of which are available to the MSH user. Unfortunately, the semantics of these methods is not visible from the shell, but a number of .NET object help is available online.
The availability of these methods creates an explosion of possibilities. For example, if I wanted to change the case of a string from lower to upper I would do the following: (first ksh and then MSH).
The UNIX example relies on the tr cmdlet.
For example, suppose the string “ABC” was to be inserted after the first character in the word “string” to have the result “sABCtring“. Here are the following, first with ksh, then with MSH:
Both examples require specific knowledge; however, using the “insert” method is more intuitive than using the capture buffers available in sed. Moreover, the domain specific knowledge of the “sed” language required may be somewhat more advanced than is required to use the Insert method.
Jim Truher and Jeffrey Snover
Jim Truher and Jeffrey Snover
[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]
PSMDTAG:FAQ: KSH BASH – how does PowerShell compare to KSH and BASH?
PSMDTAG:DOTNET: Leverage System.String to do KSH/BASH type functions.