Windows shell scripting is a powerful language that can introduce you to the world of scripting. Even if you’ve never written code before, you can quickly perform tasks that would take much longer to perform in the GUI. In the next 10 lessons, I’ll cover the basics of shell scripting. In each lesson, I’ll give you practice exercises that can help you hone your new skills. The more you practice, the sharper your new skills will become.
The shell scripting language that’s available in Windows 2000 and Windows NT is much more powerful than the shell scripting language that’s available in Windows 9x, MS-DOS, or OS/2. Some shell scripting commands in Win2K and NT don’t exist or have different or limited functionality in the other OSs. So, if you’re scripting in an environment that has Win9x, MS-DOS, or OS/2 machines, you’ll need to test your scripts carefully to make sure they function correctly.
Setting Up Your Scripting Environment
Before you start writing code, you need to set up your scripting environment. Open a command shell window, and right-click the title bar. Select Properties from the drop-down menu. On the Options tab, select the QuickEdit Mode check box. QuickEdit Mode lets you select text with your mouse and perform copy and paste operations. Next, select the Insert Mode check box. By selecting Insert Mode, you’ll insert rather than overwrite text as you type. Finally, click the Layout tab. Under Screen Buffer Size, adjust the Height to a value between 250 and 300. This height lets you scroll to see any command output that doesn’t fit in the normal window dimensions.
- After you make these adjustments, click OK. The Apply Properties dialog box that appears gives you two options. Select either Modify shortcut that started this window or Save properties for future windows with same title. (The option that the dialog box displays depends on how you launched the command shell window.)
- These modifications make the command shell window easier to work in. If you want to modify the appearance of the command shell window, you can use the options on the Font and Colors tabs in the Properties dialog box.
- To test the QuickEdit Mode you just enabled, copy the text
Echo Hello World. Here is my first line of shell scripting code!
then right-click anywhere in the command shell window. The text you copied will appear next to the command prompt. Press Enter to execute your first shell command.
@Echo Off Echo Hello World. Here is my first line of shell scripting code! Echo Hello World. Here is my second line of shell scripting code! Echo Hello World. Here is my third line of shell scripting code!
then paste them into the Notepad file. Select Save in the File menu. In the Save As dialog box that appears, type Hello.bat in the File name text box. Leave the default entry of Text Documents (*txt) in the Save as type text box. In the Save in drop-down menu, select Desktop and click Save. Close Notepad.
- These two exercises demonstrate three important scripting concepts:
- You can run only one line of code at a time from the command shell window.
- You can use a .bat file to run one or more lines of code.
- By default, lines of code in a .bat file execute sequentially from top to bottom. (You can use certain commands to change this flow, but I’ll save that topic for another lesson.)
Learning the Echo and Rem Commands
In the .bat file you executed, you might have noticed that the word Echo appears several times. Echo is a useful command that lets you display messages. As the code
Echo Hello World. Here is my first line of shell scripting code!
shows, to display text, you specify the Echo command followed by the text you want to display. Any text between Echo and the line return will appear when you run the code.
Echo Hello World. Here is my shell scripting code that demonstrates Echo On! Echo Off Echo Hello World. Here is my shell scripting code that demonstrates Echo Off!
and paste them into the Notepad file. Save the file as HelloAgain.bat. Drag the file onto the command shell window, click the window, then press Enter to run HelloAgain.bat. In the results, note that the third line of code is visible in the command shell window but not the command that launched it. As this example shows, you can strategically use the Echo Off command to send only a command’s output to the screen.
Echo On
@Echo Off
In this case, you don’t have to use Echo On to turn the command-echoing feature back on. It goes back on automatically.
Rem The comment goes here.
Any text between Rem and the line return will not be executed. Another way to comment out a line is to use a double colon (::)
:: The comment goes here.
Practice Exercises
- From Notepad, open Hello.bat. Remove the @Echo Off command or precede this command with the Rem command. Run Hello.bat to see the results.
- Remove @ from the @Echo Off line. Run Hello.bat to see the results.
- Comment out the @Echo Off line, and place @ in front of each Echo command. Run Hello.bat to see the results.
- Drag HelloAgain.bat onto the command prompt window, then press the Escape key and observe the result. (Escape clears the entry.)