Free Power Tools Brochure
Get Mark Minasi's
17-page guide today!



      

advertisement

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

January 03, 2001 02:03 PM

Shell Scripting 101, Lesson 1

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

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

    ARTICLE TOOLS

    Add a Comment

    very useful for me

    Khowal7/6/2010 12:06:08 AM


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


    Anonymous User 11/16/2004 5:45:05 AM


    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.

    Pamela D. Hudson 6/22/2004 1:01:36 PM


    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?



    Adrian Perez 6/8/2004 2:31:48 AM


    Learned something new in the first lesson. Cool.



    lavarez 2/25/2004 12:19:34 PM


    I have worked on all your 10 lessons. It was indeed very helpful.



    Khaled Ahmed 2/5/2004 11:18:02 AM


    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


    @echo off

    Echo Hello World. Here is my first line of shell scripting code!


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


    Echo Hello World. Here is my second line of shell scripting code!


    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.

    --Eve Oliver


    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.


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

    @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!


    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.

    --Dick Lewis







    eve Oliver 5/7/2002 6:17:00 AM


    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.



    rs 4/25/2002 6:59:28 AM


    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.

    --Robert Hamblin


    In Windows 2000 or Windows NT, you can open a command prompt by selecting Run in the Start menu, typing CMD, 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.
    --Dick Lewis




    Robert Hamblin 4/22/2002 12:22:22 PM


    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.



    Dave Kendall 4/22/2002 9:42:42 AM


    You must log on before posting a comment.

    Are you a new visitor? Register Here

    Setup rights to helpdesk group to unlock shared files

    Does anyone know how to setup a group or permissions for a group (helpdesk) to be able to unlock a shared network file such as a spreadsheet or Access...222-96217

    advertisement

    GOOGLE LINKS
    SPONSORED LINKS
    FEATURED LINKS

    Podcasts

    To successfully implement virtual desktops, IT administrators must carefully match user requirements to specific desktop technologies. Listen to this podcast to learn what you need to keep in mind when formulating your approach to desktop virtualization.

    Downloads

    PacketTrap IT is a comprehensive and affordable network management and application monitoring solution that solves problems associated with bandwidth, network and application performance, and connectivity. Gain insight into your network - try PacketTrapIT free for 21 days!

    Web Seminars

    Aside from its employees, data is an organization’s most important resource. Join Windows technical specialist and 11-time MVP John Savill to learn the best practices for managing data using features in Windows Server.
    View this web seminar on demand!

    eLearning Series

    We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
    Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

    Subscribe to Windows IT Pro!

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