Shell Scripting


Shell scripting involves running a series of commands from within a command shell (e.g., command prompt). Although these commands can be run from the command line individually (e.g., COPY *.*), they are more often stored within a script or batch file. A batch file is a text document with a .bat or .cmd extension. Shell scripting has been around since the inception of MS-DOS and is the easiest scripting method to learn.

Using the Command Shell

A command shell is a text-based, command interpreter application. Similar to MS-DOS, you type a command and the command shell displays a response. Windows provides two command shells : CMD.exe and COMMAND.com. The Windows command shell, CMD.exe, is a 32-bit application that contains many built-in commands (e.g., DIR, ECHO, DEL, and COPY). The MS-DOS command shell, Command.com, is a 16-bit application supplied for backwards compatibility of 16-bit DOS applications. It passes all commands to CMD.exe for processing, and it does not support long file names . Whenever possible, you should use CMD.exe because it provides better performance and stability than COMMAND.com. Figure 1.1 shows the window that is displayed when CMD.exe executes. In this case, the DOS command, DIR, is being executed.

click to expand
Figure 1.1: The CMD.exe command prompt window.
Tip  

A quick way to access a command shell is by clicking StartRun from the Windows Start menu, type "CMD", and click the "OK" button.

Shell Scripting Example

To help you better understand how shell scripting works, I've created a script to show you how to display the name of your computer. Later, I'll show you how to perform this same task using the other scripting tools that I will be introducing, KiXtart and Windows Script Host. This will really help you understand the differences (and similarities) involved in using the different scripting tools.

Displaying the Computer Name

To display the name of your local computer using shell scripting, proceed as follows :

  1. First, you'll need to create a simple script file (.bat file) and place a few commands in the file. Use a text editor and create a file named " scriptfile .bat." Here, scriptfile is the full path and file name of a script file that should contain the following commands:

     @ECHO OFF ECHO %COMPUTERNAME% 

Make sure that you save the file in the same directory that you will be running the command shell from (Step 2).

By default, the command shell displays (echoes) the called command before displaying the results. To suppress the called command and only display the results, the @ECHO OFF command is used. The last line uses the ECHO command to display the contents of the COMPUTERNAME environment variable, which is automatically set by the operating system at boot time.

  1. Once you have created and saved the .bat file as instructed in Step 1, you can run your script by starting the command shell (CMD.exe) and entering the following instruction at the command prompt:

     scriptfile 

Remember that the quickest way to run the .CMD command shell is to click StartRun from the Windows Start menu type "CMD", and click the "OK" button.

Limitations of Shell Scripting

Although shell scripting is easy, it is a limited language. This is something I hate to admit because my scripting roots date back to the good old days of MS-DOS. Shell scripting is not a collective language, but rather a language consisting of various individual executables and commands. It has limited logical statements, no debugging capabilities, limited error-handling capabilities, and no graphical interface.

When to Use Shell Scripting

Although shell scripting continues to improve over the years , it is best used for simple scripting tasks that do not require complex calculations or extensive file manipulation. To perform more powerful tasks, you should turn to another scripting tool, such as KiXtart or Windows Script Host.




Windows Admin Scripting Little Black Book
Windows Admin Scripting Little Black Book (Little Black Books (Paraglyph Press))
ISBN: 1933097108
EAN: 2147483647
Year: 2004
Pages: 89

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net