Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


January 03, 2001

Shell Scripting 101, Lesson 1

RSS
Subscribe to Windows IT Pro | See More Windows NT Shell Scripting Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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.)

    End of Article



    Reader Comments
    <p>Strictly speaking, the line<br>
    <br>
    <i>"You can turn the command-echoing feature off for just one line by preceding the Echo Off command with the at (@) sign."</i><br>
    <br>
    is correct. However, the following line<br>
    <br>
    <i>"In this case, you don’t have to use Echo On to turn the command-echoing feature back on. It goes back on automatically."</i><br>
    <br>
    is incorrect and/or misleading since not everyone will realize that @echo off (the example used) is actually 2 different commands on the same line. On the other hand, any reasonably intelligent person should be able to figure that out.</p>

    Michel Figgins February 19, 2001


    <p>This is a great beginning lesson to shell scripting. Very intuitive, and basic enough for anyone to be able to understand.</p>

    Justin September 24, 2001


    <p>Great advice about setting up the command window. I make these settings the standard whenever I configure a machine for our server room. One comment though: Why limit the screen buffer height "a value between 250 and 300"? I suggest a much higher number - say 2500 or more lines. There is nothing to lose by setting the higher number, and many shell scripts can easily spit out a large number of lines of output. There is nothing more frustrating to see an error message scroll by only to lose it when it scrolls off the end of the buffer.</p>

    Dave Kendall April 22, 2002


    <p>I seem to be missing some basic knowledge about Windows. What is "Open a command shell window"? I want to learn more and hope someone can comment on this aspect of scripting.
    <br><i>--Robert Hamblin</i></p>
    <p>In Windows 2000 or Windows NT, you can open a command prompt by selecting Run in the Start menu, typing <i>CMD</i>, and clicking OK. There should also be an icon to
    launch a command shell. In Win2K, select Programs, Accessories, Command Prompt under the Start menu. In NT 4.0, select Programs, Command Prompt in the Start menu.<br><i>--Dick Lewis</i></p>


    Robert Hamblin April 22, 2002


    <p>I understand exactly what you are feeling. Often times, as a beginner, terms are used that seem self-explanatory to us familiarized users. If you haven't already found out, a Command Shell Window in the Microsoft world equates to the MS-DOS prompt, usually found under the Start menu. If you are a beginner, you may want to start at a more novice level of reference other than this site just to introduce you to the basics.</p>

    rs April 25, 2002


    <p>Thanks for your lesson. However, I'm facing a problem: I'm finding it impossible to enter the second line of your 2nd example in the shell window. I can type<br>

    <pre>@echo off<br>
    Echo Hello World. Here is my first line of shell scripting code!</pre>

    successfully, but when I press Enter to go to the next line so that I can type the line<br>

    <pre>Echo Hello World. Here is my second line of shell scripting code!</pre>

    I find the first line is automatically copied. I don't even need to right-click for this to happen. I seem to be the only one to experience such a problem. I'll be happy if you can help me carry on with your lessons. I really want to learn.<br>
    --Eve Oliver</p>
    <p>The sample code you mentioned you are having problems with was intended to be saved to a .BAT file and run from the command prompt. It was not really intended to be run as individual entries on the command line.<br>

    To get the sample code to work, just copy and paste the text

    <pre>@Echo Off<br>
    Echo Hello World. Here is my first line of shell scripting code!<br>
    Echo Hello World. Here is my second line of shell scripting code!<br>
    Echo Hello World. Here is my third line of shell scripting code!</pre>

    into a Notepad file and save it as C:\test.bat. Then run C:\test.bat in a command shell window. You will see the three lines echoed out as the script runs.<br>
    --Dick Lewis</p>





    eve Oliver May 07, 2002


    <P>I have worked on all your 10 lessons. It was indeed very helpful.</P>

    Khaled Ahmed February 05, 2004


    <P>Learned something new in the first lesson. Cool. </P>

    lavarez February 25, 2004


    <P>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?</P>

    Adrian Perez June 08, 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:
    <!--Yahoo! Menu Service --></noscript>
    <!-- END Yahoo! Menu Service -->
    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.

    Pamela D. Hudson June 22, 2004


     See More Comments  1   2 

    You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




    Top Viewed ArticlesView all articles
    Command Prompt Tricks

    One reader shares his tip for setting up the command prompt to reflect a remote path. ...

    2009 Windows IT Pro Editors' Best and Community Choice Awards

    Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

    WinInfo Short Takes: Week of November 23, 2009

    An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


    Related Articles Shell Scripting 101, Lesson 10

    Shell Scripting 101, Lesson 9

    Shell Scripting 101, Lesson 8

    Shell Scripting 101, Lesson 7

    Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

    Related Events Check out our list of Free Email Newsletters!

    Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

    Best Practices for Managing Linux and UNIX Servers

    Building an Effective Reporting System

    Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
    Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

    Discover Windows IT Pro eLearning Series!
    Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


    Windows IT Pro Home Register FAQ for Windows WinInfo News
    Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
    SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
    Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
     
     Windows IT Pro is a Division of Penton Media Inc.
     © 2009 Penton Media, Inc. Terms of Use | Privacy Statement