“This ‘telephone’ has too many shortcomings to be seriously considered as a means of communication. The device is inherently of no value to us.” – Western Union internal memo, 1876
When you complete this section you will be able to:
Theory - This file contains the background and theory you'll need to successfully complete the lab exercises for this lesson. You should read this first.
DOS Lab - This is the Disk Operating System (DOS) lab manual. It contains activities and exercises to help you understand the theory as it applies to DOS.
Windows 98 Lab - This is the Windows 98 lab manual. It contains activities and exercises to help you understand the theory as it applies to Windows 98.
Linux Lab - This is the Linux lab manual. It contains activities and exercises to help you understand the theory as it applies to Linux.
Skill Check - This set of questions will quiz your understanding of the operating system theory and practice presented in this lesson.
Challenge - This set of advanced lab exercises is designed to help you apply your understanding to new challenges.
Scripting will make your work easier if you find yourself doing repetitive types of functions (like running the same report every day). In this lesson, you'll learn how to write a simple script that will make those mundane jobs easy.
Scripting is a method for you to write a simple program that you can run whenever you want. For example, suppose you routinely saved certain files on your computer to a floppy in order to back up those files. You could constantly type in the commands needed to copy those files (or click on all of them and use “drag and drop” to copy them to the floppy). However, you could also create a simple program that would copy all the files to the floppy for you - all you would have to do is type in the name of the program and let it fly!
In this chapter we’ll look at scripting and build some simple DOS and Linux scripts. All scripts are written with a very simple text editor. For your DOS scripts you’ll use the DOS editor and for Linux you’ll use vi. There is a vi tutorial available in your lessons and I will cover the DOS editor in that lab.
In DOS, you will build a program by creating a “batch” file. A batch file is nothing more than a file that contains a series of DOS commands. Those commands will be carried out one at a time in the same order they are placed in the file. For example, imagine the following two commands are in a batch file:
NOTE: I will write all of the batch file listings in this lab with line numbers (like the example above) in order to make it easier to discuss these files in class. However, batch files do not actually have line numbers - in fact, numbers like you see in the example above would make the batch file refuse to execute. Also, I will write all batch file commands using all upper-case letters - though DOS doesn't care whether you use upper- or lower-case letters.
If you execute (or “run”) that file, DOS would change to the Windows directory then list all the files in that directory using a wide format. It would simply follow all the commands in the batch file - one at a time - until it finished them all.
Batch files are identified by DOS because they end in the letters “.BAT”. To make a batch file executable, name it with a .BAT extension.
You may wonder where the word “batch” came from. This is a holdover from the old days of computing. When I wanted to create and run a program back when I was in college (mid 1970s) I would create the program with a whole pile of punch cards. I would then take the cards to the computer center where the operators there would stack them in a corner of the room until later. In the middle of the night (when things were quieter) they would gather up all the cards that had been submitted during the day and run a “batch” of them at once. From this beginning we still refer to a DOS script as a “batch job” (not to be confused with a “botched job” - which may be related but not relevant).
To write your first batch file, start DOS and follow these directions.
You will want to put all your batch files in your own directory so you can later find them to modify them or delete them. Using DOS, change to the root directory then make a new directory called “BATTEST”. Then change to the BATTEST directory (see Figure 1 for the command sequence).
Next, start editing a new batch file by entering:
(Note: in Figure 2 I have changed the colors for the Edit screen so they are more clearly visible in this document. When you first start Edit the background will be blue and the text will be white. You can change that color scheme if you wish by clicking on options in the menu.)
Enter the following lines into the Editor file.
Then, to save your hard work:
Notice that the file name is at the top of the “Save As” screen. It has example.bat already filled in and line two shows you are in the battest directory. These defaults are pre-filled for you since you started your edit session from the battest directory and specified that you wanted to edit example.bat.
Press the tab key until the cursor is in the “OK” button area, and then press the enter key to save your work.
Now, let’s try out the program you just wrote. Quit the editor by pressing ALT, then F, then X.
Now, from the DOS prompt (while in the battest directory), enter this command:
In Figure 5 you’ll note that first DOS listed all the .BAT files in the current directory (battest), and then listed all the .BAT files in the root directory. (Of course, your screen will be a bit different from mine - depending on how many .BAT files you have in your two directories).
This is a very simple batch script - but you can at least see what a script will do.
Now, let’s take a brief detour to look at the DOS editor. Start this part of the lesson by entering the following command (be sure you are in the battest directory):
Here’s some information about the DOS editor from Figure 6 .
The top bar is the DOS title bar. This is the title bar from the DOS screen - not the editor. There is little of note on this bar except you can see the normal Windows buttons on both the left and right edge of the bar. If you click the MSDOS logo on the left edge of the bar you will see a menu popup - the same type of menu you will get with all Windows programs. The buttons on the right edge of the bar perform the expected functions: minimize, maximize, and close.
Figure 8 is the DOS toolbar. Again, this is not part of the editor - but is part of the DOS system.
The first drop-down box on the tool bar will display a number of different font sizes. You may use any size you wish - but the window size will change and some of the DOS window sizes are pretty difficult to use. You may experiment a bit if you wish, but don’t be surprised if your DOS session becomes very difficult to read!
The next tools you’ll see in Figure 8 are to mark, copy, and paste text. After you click on “mark” (the dotted box) you can use your mouse to draw a box around a text area. Then when you click on “copy” (the icon with two pages) you will copy that text to the clipboard. Finally, you can click on “paste” (the clipboard icon) to insert the copied text into your document.
The button that has the four arrows will make the edit screen expand to fill the entire screen.
To return to a normal screen you must hold down the ALT key and press Enter.
The button with the hand is the “Properties” button. You can click that to set the various properties of the file you are working on.
The button with the large “A” on it will let you set the text style.
Under the row of buttons you’ll see a menubar. The menu includes many of the same options you’ll find on the toolbar.
For your second batch file, let’s create a file that will list all the files in a directory and sort that list by alphabetical order. This is a very simple batch file, but is useful for you to practice your new batch skill.
Open the DOS editor and edit a file called d.bat:
Type this line in the batch file (note, use the letter “O”, not the number zero):
Save the file and exit the editor. Now, while you are in the batchtest directory, hit the “d” key and Enter and see what happens.
Now you have written two batch files. But so far you haven’t done anything too impressive; so, let’s get more practical with our batch file.
In this section we’ll build a fairly complex batch file to backup all of the .bmp files in the Windows directory.
Enter the entire program into a batch file and then I'll explain what the various lines do. Be sure you are in the batchtest directory and enter:
Type these lines into bmpback.bat:
|3||REM BY GEORGE SELF|
|4||REM A program to backup .BMP files to a floppy|
|6||ECHO "*** Starting Backup ***"|
ECHO "Put a blank floppy disk in the disk drive."
|8||ECHO "WARNING! All data on this disk will be lost!"|
|11||SET /P usrname=What Is Your First Name?|
|12||FORMAT A: /Q /V:%usrname%|
|14||ECHO %usrname% > id.txt|
|15||DATE /T >> id.txt|
|18||COPY c:\windows\*.bmp *.*|
|20||DIR bmpfiles\ > index.txt|
|23||ECHO "*** All Done! ***"|
(Note: substitute your own name in line 3.)
Here's what the various lines do in this program.
The echo command determines what information gets “echoed” to the screen - or what gets written to the screen for the user to see. You can turn echo off as the batch file starts and then nothing will get written to the screen.
Perhaps you wondered why I put the “@” character at the start of line one. The easiest way to know why is to try executing the batch file but don’t use that character in front of “echo off.”
The “@” character makes just one line not echo to the screen. So, bmpback.bat now executes and line one says “Don’t display this line - turn off echo. Now, after the echo has been turned off, execute the rest of the program.”
If you are not turning on or off the echo command (like we did in line one), the command “echo” will write whatever follows it onto the screen. Thus, lines 6-8 (for example) will echo a short warning to the screen. By using the echo command, you can first turn off all writing to the screen, and then send something to the screen anyway. So, why turn off the echo? Many DOS commands will send a lot of messages to the screen and you won't want to confuse your user with those messages - so just turn them off.
A REM (that stands for “Remark”) is a way for you to document your program. A REM statement doesn’t do anything exciting. It’s just a way for you to make notes to yourself as you write your batch file. In the example above, I had you write the name of the batch file, who wrote it, and a sentence about what the batch file would do. As your batch files become more complex, you will want to comment frequently throughout the file so you can remember what you did - and why.
Let’s add a cls at the top of the batch file in order to always clear the DOS screen before we do anything else. This will eliminate any miscellaneous "junk" the user happens to have on the screen when your program starts.
After the warning on lines 6-8 are displayed, we want to pause the program so the user can find a floppy disk, put it into the machine, and get ready to finish the backup. The pause command will do just what it sounds like it should do - pause execution of the program. When the program is paused, there is a prompt to press any key to continue - and then the program starts again with the next line.
In line 11, the program creates a variable named usrname. So, what's a variable? It's just a location where the user can store some bit of information (like a name) and then use that information later in the program. Variables are commonly used in programming.
In line 11, the command "set" tells DOS to create a new variable. The "/p" switch will prompt for the value to be stored in the variable. "usrname=" is the actual name of the variable and the prompt "What Is Your First Name" is displayed on the screen for the user.
Line 11, then, will create a variable named "usrname," then display the message "What Is Your First Name," and then wait until the user types in a name.
Line 12 will format the disk in the "A:" drive. It will do a quick format (that's why you see the "/q" switch). The only really odd part of this command is the "/v:%usrname%" bit. The DOS format command includes a "/v:" switch to specify the volume label (or "name") for the disk. The "%usrname%" part tells DOS to use the variable named usrname created in line 11 to name the disk.
Lines 13-15 create a short text file named "id.txt" on the floppy disk. That file contains the user's name and the date. In line 14, you'll see where we simply echo the user's name (it's what's in the usrname variable). However, notice that I also specify "> id.txt" as part of line 14. That part of the line will redirect the results of the echo command to a file named id.txt. If that file exists, it will be overwritten; if it does not exist, it will be created.
Line 15 will add the date to id.txt. In this case, you'll note I used ">> id.txt". The double redirect symbol means to append the date to id.txt rather than create a new file.
Lines 16-18 just copy all of the .BMP files in the C:\WINDOWS directory to the floppy.
Lines 19-20 create an index of the files that were copied. In line 19, the program just changes to the root directory on the A: drive. Line 20 lists the files in the bmpfiles directory, but notice that the listing is redirected into a file called index.txt.
Lines 21-23 simply return the computer to the C:\ prompt, clear the screen, and print a short "All Done!" message.
Congratulations! You have now written a DOS batch file to backup selected files from the hard drive. That wasn't so hard, was it?
In Windows XP, you will build a program by creating a “scripting” file. A scripting file is nothing more than a file that contains a series of commands using a language of your choice (though Visual Basic Script and J-Script are the most popular). Those commands will be carried out one at a time in the same order they are placed in the file. For example, imagine the following two commands are in a scripting file:
|2||strName = InputBox("Enter Your Name: ")|
|3||WScript.echo "Hello, " & strName|
NOTE: I will write all of the scripting file listings in this lab with line numbers (like the example above) in order to make it easier to discuss these files in class. However, scripting files do not actually have line numbers - in fact, numbers like you see in the example above would make the scripting file refuse to execute. Also, I will write all scripting file commands using a peculiar case (commands like "vbCRLF") - the case is important and you should try to match it letter-for-letter.
If you execute (or “run”) the above file, Windows would pop up an input box and ask for your name. It would then pop up another box containing the sentence "Hello, " and your name. It would simply follow all the commands in the scripting file - one at a time - until it finished them all.
Scripting files are identified by Windows because of their file extensions (the last three letters). Files ending in “.vbs” are Visual Basic scripts and files ending in ".js" are J-Script files. To make a scripting file executable, name it with an appropriate extension. In this tutorial, we'll use only Visual Basic scripts, so all of the file names we use will end with ".vbs".
To write your first script follow these directions.
You will want to put all of your scripting files in the same directory so you can later find them to modify them or delete them. In "My Documents," create a folder called "scripts" to contain all of your Windows scripts.
Next, use Notepad to edit a new script. Start Notpad by clicking on Start -> Accessories -> Notepad.
Note: be careful to not use Wordpad. It adds certain "invisible" characters (like tabs) to your file that will make your scripts malfunction. Be sure to always edit scripts with Notepad.
Enter the following line into the Notepad file (like in Figure 1).
|1||WScript.echo "Hello, World!"|
Then, to save your hard work:
Note: Notepad will, by default, name all files it saves with a ".txt" extension. That extension may be appropriate for many Notepad files, but not for scripts. Be careful to click the selector arrow beside "Save as type:" at the bottom of the screen and choose "All Files" and then name your file with a ".vbs" extension (as illustrated in Figure 2 above).
Now, let’s try out the program you just wrote. Quit Notepad by clicking on File -> Exit or click the red "X" in the top right corner of the screen.
Now, navigate to My Documents -> scripts and double-click on hello.vbs (note in Figure 3 that the icon for a scripting file is different than other documents).
In Figure 4 you can see the output of the "hello.vbs" script. It may not be too exciting, but you can at least see a script in action. Next, let's do something a bit more complex with a script.
For your second script, let’s interact a bit with the user. This is a very simple script file, but is useful for you to practice your new skill.
Open Notepad and type these lines in a new document (these lines are explained below):
|2||UserName = InputBox("Enter Your Name:")|
|3||WScript.echo "Hello, " & UserName|
Save the file as name.vbs (be sure to select "All File Types" before saving) and exit Notepad. Now, double-click name.vbs in My Documents -> scripts and see what happens.
Line 1: Most scripts you write will need to store bits of information (called "variables") to be used later. For example, in name.vbs, you store the user's name so you can later display it. Visual Basic needs to have you "declare" whatever variables you intend to use at the very top of the program so Windows can set aside enough memory space for your program. Line one just alerts Windows that you intend to use a variable called "UserName" later in your program so it can reserve memory for that variable.
Line 2: This line will actually put information in the UserName variable you declared in line one. In this line, you request Windows to open an input box (like in Figure 6) and wait for the user to type in a name. Whatever the user types in gets stuffed into the variable named "UserName" for later use.
Line 3: This line uses whatever is in the variable UserName and displays that information on the screen (like in Figure 7). The command "WScript.echo" will write something to a text box on the screen. What gets written to the screen, in this case, is the word "Hello, " and whatever is in the variable UserName.
Now you have written two scripts. But so far you haven’t done anything too impressive; so, let’s get more practical.
In this section we’ll build a fairly complex script to backup all of the .bmp files in the Windows directory.
Enter the entire program into a scripting file named bmpback.vbs and then I'll explain what the various lines do.
|3||' by George Self|
|4||' A program to backup .BMP files to a floppy|
|5||' Declare all variables|
|6||dim txtName, fsoFloppy, workingFile|
' Start the process
|8||WScript.echo "Place a formatted floppy in the a:\ drive. " & _|
|9||" NOTE: This process will delete all files on that disk!"|
|10||' Create a file system object named fsoFloppy|
|11||Set fsoFloppy = CreateObject("Scripting.FileSystemObject")|
|12||' Delete all files and folders on the A:\ drive|
|13||' NOTE: I created a temp file and folder first in order to avoid|
|14||' the File Not Found error message|
|16||fsoFloppy.DeleteFile "a:\*.*", True|
|19||' Get the user's name|
|20||txtName = InputBox("Enter Your Name: ")|
|21||' Create the id.txt file|
|22||set workingFile = fsoFloppy.OpenTextFile ("a:\id.txt",2,True)|
|26||' Create the bmpfiles folder|
|28||' Copy the bmp files to the folder|
|29||fsoFloppy.CopyFile "c:\windows\*.bmp", "a:\bmpfiles"|
|30||WScript.echo "Finished With That Task!"|
(Note: substitute your own name in line 3.)
Line 1: The "Option Explicit" command tells Windows that all variables must be explicitly declared before being used. This is a wonderful option that will keep you from accidentally mistyping the name of a variable and then spending hours troubleshooting.
Lines 2-4: These are just standard header lines for a program.
Line 6: This is where all variables are "declared" before being used. By declaring your variables before using them you will make the program more efficient and will keep from accidentally mistyping a variable's name.
Lines 8-9: This just presents a short warning for your user.
Line 11: This line creates a file system object. It's the object that has all of the various tools available for your to use (like copying files). The file system object we'll use throughout the program is named fsoFloppy.
Lines 15-18: I wanted to delete everything on the floppy disk. However, if the disk were already clean then I would get an error message and not be able to continue. These lines first create a small "dummy" file and then delete all files; and then create a small "dummy" folder and then delete all folders.
Line 20: This is where the visitor can input a name, to be used in the ID.TXT file.
Lines 22-25: Here I create a small text file with two lines: the user's name and the date.
Line 27: In this line I create a folder on the floppy disk to hold all of the .BMP files.
Line 29: Here is where I actually copy all .BMP files from the Windows folder to the floppy disk.
Line 30: This is just a simple message to let the user know the program is finished.
Congratulations! You have now written several small scripting files. That wasn't so hard, was it?
Cochise College has a full semester class on Unix Shell Scripting so I will not attempt to cover much of it here. We will create a simple script using vi, then execute that script. My goal is not to make you a proficient shell scripter (that would take a long time), but to simply show you that you can write - and execute - a script in Linux.
For this exercise, start Linux, change to your CIS 140 directory, and then use vi to open a new file called report. Enter these lines into report:
echo Report of login names and users on this
cut –f 1,6,7 –d: /etc/passwd | sed –e ‘s/:/ - /’ | sort | more
I know that last line looks funny, but what it will do is “cut” all the user names, starting directories, and startup shells from the password file. It will then sort that information alphabetically and present it one screen-full at a time.
To actually execute the report you need to save it and exit vi. To make it executable, enter this command:
chmod 777 report
The chmod command will change the "mode" (or "security level") of the file so anyone can execute it. Then, actually run the report with this command:
Note: you need to preface the script with “sh” in order to get Linux to execute the report in a new, temporary shell.
When the report script executes it will present you with a list of the users on your system, their startup shells, and their home directories.
Congratulations. You have now written you first Linux script. Now that wasn't too hard, was it?