OPS145 Lab 5 Newversion: Difference between revisions

From Littlesvr Wiki
Jump to navigation Jump to search
(Replaced content with "Category:OPS145 The latest version is here: OPS145_Lab_5")
Tags: Replaced Visual edit
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
[[Category:OPS145]]
[[Category:OPS145]]
 
The latest version is here: [[OPS145_Lab_5]]
= !!! THIS PAGE IS NOT READY YET !!! =
One of the most common tasks a system administrator will do is to modify some configuration. Almost all services on Linux servers are configured using plain text configuration files.
 
So far in the course you've been using a graphical text editor. But since Linux servers don't have graphical user interfaces (GUIs): you have to learn to edit text files on the command line.
 
There are several common applications for command line text file editing. We're going to learn '''vi'''. Even though vi is a frustratingly annoying application to use: it's the one available on any Linux system.
 
= vi =
As you go through your Linux courses: try to think of vi as a necessary evil. We're going to learn the absolute basics necessary for you to be able to accomplish any task a systems administrator would need it for.
 
== Modes ==
One of the confusing things about vi is that it can be in one of two modes at any time: command mode, and insert mode.
 
In '''insert mode''' it functions more or less the way you'd expect a text editor to function, except depending on your setup: the arrow keys, delete key, and backspace key might do weird things. Otherwise: when you press a letter key: that gets inserted into the text.
 
In '''command mode''' the keypresses on your keyboard are interpreted as instructions/commands to vi, and different keys are used for different commands.
 
For example: in insert mode pressing '''x''' on your keyboard will put the letter x into the text where the cursor is, but in command mode pressing '''x''' on your keyboard will delete the character underneath the cursor.
 
Often times this creates such confusion (especially if you don't look at the screen when you're typing) that you have to abandon what you've been trying to do and start over. A good portion of this lab is spent on recovery from mistakes.
 
== Quit without saving ==
 
* Open one of the text files you donwloaded in lab2 in vi:<syntaxhighlight lang="bash">
vi ~/Downloads/SampleFiles/1984.txt
</syntaxhighlight>
 
Similarly to the '''less''' command: you can move around using the arrows, home/end, and PgUp/PgDown keys. Unlike the less command: there is a cursor. But if you try to type in some text: depending on which keys you use either nothing will happen or something weird will happen.
 
* Move the cursor to an empty like, and type in '''hello'''. Whatever happend in vi is probably not what you wanted.
* Press '''ctrl+s'''. This will not save your document. Try '''ctrl+w''', and '''ctrl+q'''. Neither will quit out of vi.
* Try the '''backspace''' and '''delete''' keys. They may or may not work, depending on various complicated factors.
 
At this point you probably made some unintended changes to your document, and you don't know how to undo them. The best thing to do when you're in this situation is to quit vi without saving:
 
* Press the '''Escape''' key, then colon (''':'''), then '''q''', then exclamation mark ('''!'''), then '''Enter'''. From now on in the instructions I'll use a shorthand notation like this: '''Esc'''+''':q!'''
 
This will quit vi without saving any changes you made to the document.
 
* You will also want to do this if you open vi with no filename, or the wrong filename. Try it:<syntaxhighlight lang="bash">
vi ~/Downloads/SampleFiles/1985.txt
</syntaxhighlight>
* You could put in some text, and save it: but it would be saved in the wrong file. So quit without saving using '''Esc'''+''':q!'''
{{Admon/important|Never use ctrl+z|If you've taken this course with someone else: you may have learned about ctrl+z. '''You are not qualified to understand what that does, and using it will cause you more trouble than good.''' Just forget that that key combination exists!}}
 
== Switching modes ==
 
* Open 1984.txt again, use an absolute path for practice:<syntaxhighlight lang="bash">
vi /home/yourusername/Downloads/SampleFiles/1984.txt
</syntaxhighlight>
* Move your cursor to the fourth line using the arrow keys.
* Press '''i''' - this will switch vi into '''insert mode'''. Depending on the version of vi: it may or may not tell you at the bottom that it's now in insert mode.
* Type in '''This is a new sentence!''' - this will insert that text at the cursor position.
* Press '''Escape'''. This will take vi into '''command mode'''.
* Save your changes using the ''':w''' command.
* Quit vi using the ''':q''' command.
* Use '''less''' to read 1984.txt and confirm that your changes have been saved.
 
== Append ==
The '''i''' command will switch vi into insert mode, and will keep the cursor where it is. Sometimes you need to insert some text at the end of a line, and because of your configuration the arrow keys won't work in insert mode. Try it:
 
* Open 1984.txt again using vi
* Move your cursor to the end of the line you typed in (over the exclamation mark), and switch to insert mode using '''i'''.
 
Note that the cursor is over the exclamation mark, but you will need to add text after it.
 
* Try to move the cursor to the right using the arrow key. If your configuration is the same as my defaults: vi will do something stupid.
* Quit vi without saving your changes.
 
There's another command which will switch vi into insert mode, but unlike '''i''': it will also move the cursor one character forward. That command is '''a''' (for append). Try it:
 
* Open 1984.txt again using vi
* Move your cursor to the end of the line you typed in (over the exclamation mark), and switch to insert mode using '''a'''.
* Type in " '''This is a second sentence. And a third.'''" You should end up with this:
 
[[File:ViInsertedTwoSentences.png|center]]
 
* Save your changes, exit vi, and confirm that your changes were saved correctly.
 
== Delete one character ==
Sometimes the delete and backspace keys work properly, but half the time they dont. A relatively easy way to delete some text is to use the '''x''' command, in command mode.
 
*Open 1984.txt again using vi
* Move your cursor to the space between the second and third sentence you added.
* Still in command mode: press '''x'''. This will delete the space.
* Press '''x''' several more times to delete the rest of the third sentence.
 
If you make a mistake (e.g. delete too much): quit vi without saving, and try again.
 
You should end up with this:
[[File:ViThirdSentenceDeleted.png|center]]
 
* Save your changes, exit vi, and confirm that your changes were saved correctly.
 
== Search ==
If the file you're editing is more than a couple of pages long: you'll probably want to use the search function. It's relatively painless, just remember that it does a case-sensitive search.
 
* Open 1984.txt again using vi
 
You're going to have to find a note at the end of the book. That note contains the string '''Australia'''.
 
* Still in command mode: press the forward slash ('''/''') followed by '''Australia''', then '''Enter'''
* The first place the string "Australia" shows up is not what you're looking for. Find the next occurence using just slash and enter.
* You should end up with your cursor on one of the last lines in the file:
 
[[File:ViAustraliaFound.png|center]]
 
* Leave it there for the next part you need to do.
 
== Delete one line ==
When you need to delete a whole line (rather than part of one): you can use the '''dd''' vi command. I don't know what that stands for. Delete-da-line or something.
 
* With your cursor anywhere on the line with Project Gutenberg: press '''dd''' (press '''d''' twice). That line will be deleted.
* Repeat the command a few times so that only one empty line is left after THE END:
 
[[File:ViLastLinesDeleted.png|center]]
 
* Save your changes, exit vi, and confirm that your changes were saved correctly. In the '''less''' command you can press the '''End''' key to go to the end of the file.
 
== Undo ==
If you've been working on a file for some time, and then accidentally press the wrong key (for example because the arrow keys don't work): you can use the undo feature to avoid redoing all your edits. It only undoes the one last operation performed but that can still be helpful. Try it out:
 
* Open 1984.txt again using vi.
* Switch to insert mode.
* Press one of the arrow keys. If your vi is in the same default configuration as mine: instead of the cursor moving it will insert a new line with a character on it.
* Switch back to command mode.
* Press '''u''' - it should remove the extra line with the extra character.
* The second time you press '''u''' will redo whatever was undone.
 
= More vi practice =
 
* Download this file: [http://ops345.ca/ops145/ViExercise.tar.xz ViExercise.tar.xz] and extract it in the Downloads directory.
 
The tarball contains a directory which you don't have read permissions to. This is to encourage you to use vi to complete this section of the lab instead of using a graphical text editor. Of course if you're stubborn: you have learned by now how to change permissions on files which you own.
 
* Open the file ~/Downloads/ViExercise/Practice.txt using vi
* The instructions for what you need to do are in the file. Try not to make unnecessary changes, since the script checking your work isn't very intelligent.
* Save your changes, and quit vi. (You can save and exit at the same time with the ''':wq''' command).
 
= First half of the course: review =
In class with me: we will use any lecture time remaining to review whatever parts you like from the first half of the course.
 
=Submit evidence of your work=
After you finish the lab: run the following commands to submit your work:<syntaxhighlight lang="bash">
cd ~
wget http://ops345.ca/check/ops145-lab5-check.sh
chmod 700 ops145-lab5-check.sh
./ops145-lab5-check.sh
</syntaxhighlight>If it says "Your lab5 has been submitted": make a screenshot, and you're done. If it gives you any warnings or errors: you have to fix them and try the ./ops145-lab4-check.sh command again.

Latest revision as of 23:40, 10 February 2024

The latest version is here: OPS145_Lab_5