Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  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 log on before posting a comment.

    If you don't have a username & password, please register now.




    Top Viewed ArticlesView all articles
    Friday at PASS Europe 2006

    Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

    More fun TechEd 2005 Resources

    Kevin points out some more TechEd resources ...

    Outlook Tips and Techniques

    Read about hiding items, merging appointments, multiple windows, creating views, permissions, sending Outlook items to outside recipients, Send As permission, Inbox Assistant, tricks for rules, and tips for obtaining Microsoft Knowledge Base articles. ...


    Related Articles Shell Scripting 101, Lesson 10

    Shell Scripting 101, Lesson 9

    Shell Scripting 101, Lesson 8

    Shell Scripting 101, Lesson 7

    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 Become a VIP member of the Windows IT Pro community!
    Get it all with the VIP CD and VIP access. A $500+ value for only $279!

    Subscribe to Windows IT Pro!
    Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

    Monthly Online Pass - Only $5.95!
    Get instant access to 10,000+ articles from Windows IT Pro Magazine!

    TechNet Virtual Labs
    Evaluate and test Microsoft's newest products.

    Job Openings in IT


    ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

    Microsoft Exchange & Windows Connections event returns to Las Vegas Nov 10 - 13
    Connections returns to Las Vegas for this exciting event where each attendee will receive SQL Server 2008 standard with 1 CAL. Co-located with Microsoft ASP.NET, SQL Server, and SharePoint Connections with over 250 in-depth sessions.

    Free Online Event! Virtualization:Get the Facts!
    Register now and attend this free, live in-depth online conference on November 13 and 20, 2008, produced by Windows IT Pro. All registrants are eligible to receive a complimentary one-year digital subscription to Windows IT Pro (a $49.95 value)!

    Check Out Hyper-V Video on ITTV
    Watch Karen Forster's interview on Hyper-V's performance on ITTV.net.

    Ease Your Scripting Pains with the Flexibility of PowerShell!
    Join MVP Paul Robichaux on December 11, 2008 at 11:00 AM EDT as he equips you with PowerShell basics in 3 introductory lessons, each followed by a live Q&A session—all on your own computer!

    PASS Community Summit 2008 in Seattle on Nov 18-21
    The don’t-miss event for Microsoft SQL Server Professionals. Register now and you’ll enjoy top-notch Microsoft and Community speakers and more.



    Solving PST Management Problems
    In this white paper, read about the top PST issues and how to administer local/network PST Files.

    Get Protected -- Data Protection Manager 2007
    Protect your virtualized environment with Data Protection Manager

    Order Your SQL Fundamentals CD Today!
    Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

    Maximize Your SharePoint Investment: Get Your Data Moving
    Watch this web seminar now to learn how to maximize your SharePoint investment! Join us as we take a look at the complex business of securing, accessing and managing vast amounts of information in a global network and various ways to get your data moving.
    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 Windows Dev Pro IT Job Hound ITTV
    IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
     
     Windows IT Pro is a Division of Penton Media Inc.
     Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing