Creating Command Line Scripts
Command line scripts are text files containing the commands
you want to execute. These are the same commands you would normally type into
the Windows command shell. However, rather than enter the commands each time you
want to use them, you create a script to store the commands for easy
execution.
Because scripts contain standard text characters, you can create
and edit scripts using a standard text editor, such as Notepad. When you enter
commands, be sure to place each command or group of commands that should be
executed together on a new line. This ensures proper execution of the commands.
When you have finished creating a command-line script, save the script file
using the .bat or .cmd extension. Both extensions work with command-line scripts
in the same way. For example, if you wanted to create a script to display the
system name, Windows version and IP configuration, you could enter these three
commands into a file called SysInfo.bat or SysInfo.cmd:
hostnameveripconfig -all
Once you save the script, you can execute it as if it were a
Windows utility; simply type the name of the script in a command shell and press
Enter. When you do this, the command shell reads the script file and executes
its commands one by one. It stops executing the script when it reaches the end
of the file or reads an EXIT command. For the example script, the command line
would display output similar to Listing 3-1.
Listing 3-1: Output of Sample Script
C:\>hostnamemailer1C:\>verMicrosoft Windows [Version 5.2.3790]C:\>ipconfig -allWindows IP ConfigurationHost Name . . . . . . . . . . . . : mailer1Primary Dns Suffix . . . . . . . : adatum.comNode Type . . . . . . . . . . . . : UnknownIP Routing Enabled. . . . . . . . : NoWINS Proxy Enabled. . . . . . . . : NoDNS Suffix Search List. . . . . . : adatum.comEthernet adapter Local Area Connection:Connection-specific DNS Suffix . :Description . . . . . . . . . . . : Intel(R) PRO/100 VE NetworkConnectionPhysical Address. . . . . . . . . : X0-EF-D7-AB-E2-1EDHCP Enabled. . . . . . . . . . . : NoIP Address. . . . . . . . . . . . : 192.168.10.50Subnet Mask . . . . . . . . . . . : 255.255.255.0Default Gateway . . . . . . . . . : 192.168.10.1DNS Servers . . . . . . . . . . . : 192.168.10.155
If you examine the listing, you’ll see that the
command prompt and the actual commands are displayed as well as the output of
the commands themselves. The reason for this is that the command shell does some
extra work behind the scenes while executing scripts in the default processing
mode. First, the command shell displays the command prompt. Next, it reads a
line from the script, displays it, and then interprets it. If the command shell
reaches the end of the file or reads an EXIT command, execution stops.
Otherwise, the command shell starts this process all over again by displaying
the prompt and preparing to read the next line in the script.
Although the default processing mode with command echoing on
can be useful for troubleshooting problems in scripts, you probably don’t want
to use this display mode with scripts you’ll use regularly. Fortunately, you can
change the default behavior by turning command echo off, as I’ll show you later
in the chapter in the section titled “Managing Text Display and Command
Echoing.”
No comments:
Post a Comment