– Fall 2010 IT442 Bruce Mahfood Project 4 Part 3

advertisement
IT442 – Fall 2010
Bruce Mahfood
Project 4 Part 3 (Due 10/25) – Command Line Interface
Objectives:

Getting comfortable with use of the command line (CLI):
o Getting information about a server system.
o Configuring a server system.
o Automation of routine tasks using CLI batch files.
Book references: The reading for this project is found in Chapter 6, pp. 107-134.
Lab activities:


Make sure you have gone over the book’s coverage of Simple Batch Files on pp. 131134. The link http://ss64.com/nt/ gives you a good reference to a large number of
available batch commands (NOTE: remember that you can see a full list of commands
that can be run at the Command Prompt by typing HELP, and a description of individual
commands by typing HELP <command>). You can reference this link or type help for
commands as you continue working below.
Take a look at the following link to learn a little more about creating batch files:
http://www.computerhope.com/batch.htm
o Go through the list of batch commands given at the first link site (by no means an
exhaustive list as you can see from the second link given) and make sure you
understand what each command will do for you.
o Go to the section labeled Running Different Programs. There is an example
batch file given. Open a file on your desktop named mychoice.bat, and either
copy/paste or type this text into it as given.
 Before running the batch file, make the following changes:
 For the choice command, the script given has one error. It gives
some text that is to be shown as the prompt: “Pick a number (1, 2,
or 3)%1”, but this text must be preceded with the /M option and a
space, and must be in double quotes. The %1 is unnecessary in
this example and can be left out.
 Save the file and open a Command Prompt. Change directory to the
Desktop folder, and then type “choice<enter>”. This will run the program
which will prompt you for your choice of 1, 2, or 3. It will take any one of
1

those choices, without an <enter>, display the corresponding text, and
exit. Making different choices will display different text.
o Under the heading How To Make A Time Log, there are three ways that are
given for adding a time stamp to a log file from a batch file. Try the first and third
(the second does not work on NT systems). You will see that the first has a
prompt given afterward, but since the third method runs the output through a find
for the line that has the string “current”, the time stamp from the third method is
correct for putting inside a log.
o The section called Technical Support contains links with more information. Take
a look at the following links. You can take a look at these one your own if you
would like.
Take a look at the following links (these follow one after the other) for more on batch
files:
http://commandwindows.com/batch.htm
http://commandwindows.com/batchfiles-branching.htm
http://commandwindows.com/batchfiles-iterating.htm
http://commandwindows.com/variables.htm
o Try the example lines as you see them. It is one thing to read a command line,
but many times it is more meaningful when you run the command yourself.
Written Assignment – Part 3:
The written narrative for this assignment, as stated in part 1, will contain the work done for all
three parts of this project. The following questions are for your homework assignment, also due
on 10/20. The narrative is to be a separate document to the written assignment for
answering questions:
1) What is the difference between an internal and an external command? These are two
types of commands that can be typed in at the command line in a CMD.EXE (Command
Prompt) window. Why is an internal command always available to you from whichever
directory you happen to be in while in the Command Prompt, and conversely, why is an
external command not always available depending on which directory you are in?
2) How does the Command Prompt use the Path variable to run commands from the
command prompt? What can you say about the content of the Path variable if the
command that is given is not found? How can the contents of the Path variable be
ordered for greatest efficiency in running commands?
3) What do square brackets mean for describing command syntax? What does the ‘|’
character mean for describing command syntax? These both have a very particular
meaning. In your own words, describe exactly what the following make-believe syntax
means. I am not asking for what the –p and –o options mean, but what the overall
syntax means (in other words something like “There is a command called mycommand
that takes two options, -p and –o, which …”:
2
mycommand [ –p [ –o [ OPTION1 | OPTION2 ] ]
4) The batch file that you created above called choice.bat runs through once, with no
looping. I want you to rework the batch file so that it does the following:
a. If either 1 or 2 is pressed, it will tell me that they were pressed, just as it does
right now, and then it will go back and prompt again for another input.
b. If 3 is pressed, it will tell me that three was pressed, and it will exit the program.
c. Be very sure to test your batch file for all three cases before adding it to your
homework. Put a rem command at the top as a comment, which will state your
name, the project number, and the question number. Then attach a copy of this
file as question 4 of your homework, and send a copy of the batch file to me at
kbmahf@gmail.com so that it can be tested.
5) Write two simple batch files that do the following:
a. File 1 is to be called direct.bat:
i. Turn off echoing at the start
ii. Write a FOR loop that looks for all directories directly under C:\ (look at
the FOR loop description on http://commandwindows.com/batchfilesiterating.htm - the command you will issue needs to be slightly different
than this) with a given pattern in their name. The pattern will be passed in
from the command line, and you should match any directory which has
that pattern found somewhere in the name. It could be the whole name
itself, or it could be just one letter in the name.
iii. The for loop runs through each directory name found and echoes the
name to standard out (the display, when this batch file is run by itself).
iv. When run on its own, you must be able to give a full name or a letter to
match directories found directly under C:\, and having run this command,
the correct list of directories from C:\ will be displayed, each on a
separate line.
v. This batch file only needs two lines of code to do what I have asked.
b. File 2 is to be called loopdirect.bat
i. Turn off echoing at the start
ii. Write a loop using GOTO statements. The content of the loop is as
follows:
1. The batch file prompts the user using the CHOICE command to
enter a ‘q’ to quit the batch run, or a ‘c’ to continue. The prompt
must look as follows: “[<q>uit | <c>ontinue]” so that the user
knows that q means quit, and c means continue.
2. If the user enters a ‘c’ to continue, the batch file prompts the user
using the SET command, for a file pattern that is to be used in a
call to direct.bat.
a. NOTE: it is easiest to only test for q to quit, and otherwise,
even if another character is given do the call. I would
rather that if another character other than q or c is given,
an error message is shown, and the prompt for continuing
is given again. I am not going to require this, but it is a
3
good exercise for you to add some error checking into your
batch code.
3. Once the file pattern has been received into a batch variable, the
batch file calls direct.bat using the CALL statement and passing
the variable as its command line argument. This call prints its
output to the display.
4. Follow this call with another identical call whose output must be
redirected to append to the end of a file called direct.log.
5. Before each append of directory data to the file direct.log, the
batch file must first get a reasonable timestamp string into a
variable, as shown in the lab material above, and append first a
blank line and then the timestamp to the end of the direct.log file.
The directory name data will be appended to the file after that.
iii. Be very sure to test your batch file for all three cases before adding it to
your homework. Put a rem command at the top as a comment, which will
state your name, the project number, and the question number. Then
attach a copy of this file as question 5 of your homework, and send a
copy of the batch file to me at kbmahf@gmail.com so that it can be
tested.
4
Download