Introduction to Operating Systems Lab 1 Command Line ...

Bridges To Computing

Brooklyn College

M. Meyer, 2009

Introduction to Operating Systems

Lab 1 ¨C Command Line Programming

1 Introduction:

The command line interface (or command line shell) is a mainstay for system

administrators, power users and hackers as it allows them to interact rapidly with a

computer without having to click through numerous graphic menus. Moreover groups of

instructions to the command line interface can be assembled together into what are

known as batch (or script) files. These batch files allow the automation of complex tasks,

and are analogous to primitive programs. Should you ever find yourself repeating a series

of basic instructions to a computer (moving and renaming files for example) you might

be able to save yourself a lot of time and effort by creating a batch file program to

automate that task.

Today we will explore some aspects of the windows command line interface and make a

simple game using a batch file. For more information on Windows command line

programming, feel free to check out these online resources:

? Complete List of Windows Prompt Commands

? Windows Commands by Operating System

? Princeton Tutorials on Using Windows Commands

1.1 Starting the command line window

The command prompt is run from its own window by invoking the command

interpreter that is provided by the file cmd.exe located in the Windows\System32

folder. There are two basic ways to start the command line interface:

? The command prompt window can be opened by entering "cmd" (without

quotes) into Start-Run box.

? The command prompt window can be opened going to Start then All

Programs then Accessories and then selecting Command Prompt.

A black and white window (the colors can be changed) containing the command

prompt will open.



1

Bridges To Computing

Brooklyn College

M. Meyer, 2009

2 Basic Commands

The command prompt is an interface to the operating system; the first thing you will need

to learn is how to get around.

2.1 Finding the current directory

The cd command can be used is to determine your location in the file systems. If you

type cd (lowercase) into the command prompt window and hit return you should see

something like the following:

C:\Users\meyer>cd

C:\Users\meyer

C:\Users\meyer>

The first line is what is called the command prompt. It ends with a > and by default

shows the users location in the file structure. The result of typing in the cd command

and hitting enter is displayed on the second line, and the third line is the command

prompt again. It may seem silly that the cd command gave us the same information

that is already in the prompt, but that is only because we have not changed the

prompt.

2.2 Changing the command prompt

You can change the command prompt line (hereafter called the 'prompt') to say

anything you wish. Let¡¯s do that now. Type in the following and hit return:

prompt awaiting instructions:

Your command prompt line should now say "awaiting instructions: " You can change

it to say anything you want. It is sometimes useful to have the prompt display the

time, which can be accomplished by typing the following:

prompt $T

To see even more options for setting the prompt type in the following and hit return:

help prompt

You can play with those options if you wish, but for now let's return the prompt to its

original state by typing in the following and hitting return:

prompt $P$F



2

Bridges To Computing

Brooklyn College

M. Meyer, 2009

2.3 Looking Around using dir

Type in the following and hit return

dir

The directory command (dir) will give you a listing of all the files in the current

folder. You can find out more about the dir command by typing in:

help dir

I like to use the "wide list" format with the directory command which looks like this:

dir /w

2.4 Moving around using cd

When you are done exploring the dir command we need to work our way to the

desktop folder (which is the folder that contains the icons on the 'desktop' that you

see when working in the windows Graphic User Interface).

We have already used the cd command to identify the current directory; you can

also use the cd command to change directories. Type the following and hit enter:

cd Desktop

You should now be in the Desktop folder and you should be able to confirm this by

looking at the prompt (if you are not using the standard prompt then type cd by

itself as you did in section 2.1 to confirm). Let's go back to the directory we just

left, which you can do by typing the following:

cd ..

The two dots ( .. ) tell the command to go back up one folder, and thus you should

be back in the directory you started on. Let's go back to the Desktop folder but this

time let's do it without typing in the full name Desktop. Instead type in the

following:

cd De

and then hit the TAB key. What happened? Hit enter to proceed to change to the

Desktop directory. For more information about the cd command use the help

command we used in sections 2.3 and 2.4.

2.5 Creating a new directory

You should be in the desktop folder (you can confirm this by typing cd) where we

would like to create a new folder to hold the remaining files we will create in this



3

Bridges To Computing

Brooklyn College

M. Meyer, 2009

lab. You can create a new folder (name lab1) by typing the following and hitting

enter:

mkdir lab1

If you look at your desktop you should now see a new folder called lab1. Use the

cd command to move to the lab1 folder.

2.6 Other commands

There are many other useful commands that you can use to create, move, modify

and delete files and folders. In fact anything you can do using the windows GUI

you can also do using the command line interface.

Instructions for using the other command line interface (CLI) commands can be

found online at:



A complete list of the command available to you in the CLI can also be obtained by

typing in the following command (and hitting return):

help

Before we go, try one last command, and make a note of what it does:

cls

3 Starting programs:

Windows keeps track of the names of the programs that are installed on it, and can search

for and try and load programs by name. In the next section we are going to want to create

a batchfile using the program called Notepad (which comes built-in to Windows). We can

start the Notepad program from the CLI simply by typing the name of the program

(Notepad) and then hitting enter. However, it is worth noting that many programs can

also be given "input parameters" which are additional instructions to use as the program

starts up. For example we can start the Notepad program AND get it to create and name a

file called mygame.bat for us by typing in the following command and hitting enter:

notepad mygame.bat

You should now see something like the following (note: you may get an error message

saying that the file mygame.bat cannot be found, and you should tell windows to create

that file):



4

Bridges To Computing

Brooklyn College

M. Meyer, 2009

4 Creating a batch file

A batch file (or script file) is simply a text file which contains a list of commands

designed to be run by a CLI. We are going to create a batch file today that will act as a

very simple game. In the process of creating this game we will see that the windows

command line environment is effectively a programming language environment as we

have the ability to create variables and we can access the SEQUENCE, SELECTION and

REPETITION operations necessary to support the Imperative paradigm.

4.1 Planning the game

You should NEVER coding a game (or any other large programming project)

without first planning out the flow of the game and accounting for any special

resources that the game will require.

We would like to create a simple number game, where the computer picks a

number at random, and the player tries to guess that number using a limited number

of guesses. After the computer has picked a random number the player will get 6

guesses. After the player makes a guess, if it is incorrect, the computer will tell the

player if the guess was too high, or too low. If the player correctly guesses the

number they will win, but if after 6 guesses they fail to correctly guess the number,

then the player will lose.

Take a moment and thing about the program you are going to create. Try and write

down a pseudo-code program below, making sure to clearly identify the places

where SELECTION (making a choice in the program) and REPETITION

(repeating sections of code) will come into play:

START

END



5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download