Subscribe to Windows IT Pro
January 03, 2001 12:00 AM

Shell Scripting 101, Lesson 1

Windows IT Pro
InstantDoc ID #16355
Rating: (6)

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.

  • You’ve just run code from the command line. Now, let’s use a script (i.e., a .bat file) to run similar code. Open Notepad. Copy these four lines
  • @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.

  • The file Hello.bat now appears on your desktop. Position the file and your command shell window so that both are visible on your screen. Drag the file onto the command shell window. The path to the .bat file you just created appears at the command prompt ready to run. Dragging the file onto the command shell window is a shortcut for typing the path to the file. Click the command shell window so that you see the cursor, then press Enter to run the .bat file.
    • 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.

  • You can use the Echo command to turn the system’s command-echoing feature on and off. By default, the command-echoing feature is on. To turn the system’s command-echoing feature off, you use the Off parameter with the command name. To see the command-echoing feature and the Echo Off command in action, open Notepad. Copy the lines
  • 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.

  • Like a light switch, after you turn the command-echoing feature off, it stays off until you turn it back on. To turn the command-echoing feature back on, you use the Echo command with the On parameter:
  • Echo On
  • You can turn the command-echoing feature off for just one line by preceding the Echo Off command with the at (@) sign:
  • @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.

  • Another useful command to learn is Rem. This command lets you insert remarks (i.e., comments) in a .bat file. A comment is text that’s not meant to be executed but rather to help explain something in the .bat file. Systems administrators often use comments to explain how a .bat file works or how to configure the .bat file for a particular system. Using the Rem command is simple. At the beginning of the line, you specify the command followed by the comment
  • 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

    1. 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.
    2. Remove @ from the @Echo Off line. Run Hello.bat to see the results.
    3. Comment out the @Echo Off line, and place @ in front of each Echo command. Run Hello.bat to see the results.
    4. Drag HelloAgain.bat onto the command prompt window, then press the Escape key and observe the result. (Escape clears the entry.)

    Related Content:

    ARTICLE TOOLS

    Comments
    • Khowal
      2 years ago
      Jul 06, 2010

      very useful for me

    • Anonymous User
      8 years ago
      Nov 16, 2004

      Thank You for this site.
      I have found a LOT of useful info.

    • Pamela D. Hudson
      8 years ago
      Jun 22, 2004

      Hi, I have Win98 and subscribe to GeoCities Plus (Yahoo!). Recently, I'm told by a Yahoo! Customer Service fellow that I accidentlly 'hard coded' a popup advertisement somewhere in the code, presumably at my pages. I can't find it anywhere. It seems to be on my computer!? This *** popup is following me everywhere and by passing the pop swatter software I use.
      This is the code the fellow gave me but I have no clue what to do with it. It is suppose to be hard coded at my GeoCities webpages (pameladhudson) but I can't find it anywhere:


      I'm suppose to erase everything between that code but I can't find it anywhere at my Yahoo! pages. Can you help me please?
      Thank you so much for this service on-line. I'm sure you are helping others.

    • Adrian Perez
      8 years ago
      Jun 08, 2004

      I have a Windows XP and I've read the first three lessons and I've learned, but I just don't understand what all this is for. Of what I have seen and done is getting my computer name, username, and the userprofile. I wonder how you get the IP address and all that!! What is all this really used for?

    • lavarez
      8 years ago
      Feb 25, 2004

      Learned something new in the first lesson. Cool.

    You must log on before posting a comment.

    Are you a new visitor? Register Here

    advertisement

    advertisement

    Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.