+ All Categories
Home > Documents > Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... ·...

Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... ·...

Date post: 24-May-2020
Category:
Upload: others
View: 11 times
Download: 0 times
Share this document with a friend
13
M7 Cloud Infrastructure and Operations 1 Lab 3a Using the vi editor Objectives: • Become familiar with the vi Editor • Review the three vi Modes • Review keystrokes to move between vi modes • Create a new file with vi Editor • Invoke vi with show mode • Review the save and quit commands • Use editing commands • Use search commands Scenario In this lab, the student will use a Linux text-editing tool called the vi (pronounced "vee eye") Editor. This text editor is primarily used for creating and modifying files that customize the work environment and for writing script files to automate tasks. System administrators use text editors to create and modify system files used for networking, security, application sharing, and so on. For users learning to become system administrators, it is important to know how to use the vi Editor. It is sometimes the only full screen editor available to edit crucial system files. Examples of these include scripts and environment control files. Skill in using vi Editor is also needed if the windowing system is not available. The vi Editor is a useful tool when working remotely on other Linux workstations or servers. Administrators routinely login remotely or telnet to another Linux computer to perform maintenance and troubleshooting tasks using the vi Editor. The availability and operation of vi Editor is consistent across all UNIX/Linux platforms. Step 1: Log in to Linux as root and start a terminal window click Application > System Tools > Terminal . Step 2: Launch vi by typing “vi studentvi” The vi Editor is started from the command line. Whenever invoking vi Editor, the user is opening a file. The name of the file to be created or edited can be specified when the user starts vi Editor, a new file can be opened to be named later. It is common to start vi Editor and specify a file name. If the file exists, it is opened for editing. If the file does not exist, it is created.
Transcript
Page 1: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

1

Lab 3a Using the vi editor • Objectives: • Become familiar with the vi Editor • Review the three vi Modes • Review keystrokes to move between vi modes • Create a new file with vi Editor • Invoke vi with show mode • Review the save and quit commands • Use editing commands • Use search commands Scenario In this lab, the student will use a Linux text-editing tool called the vi (pronounced "vee eye") Editor. This text editor is primarily used for creating and modifying files that customize the work environment and for writing script files to automate tasks. System administrators use text editors to create and modify system files used for networking, security, application sharing, and so on. For users learning to become system administrators, it is important to know how to use the vi Editor. It is sometimes the only full screen editor available to edit crucial system files. Examples of these include scripts and environment control files. Skill in using vi Editor is also needed if the windowing system is not available. The vi Editor is a useful tool when working remotely on other Linux workstations or servers. Administrators routinely login remotely or telnet to another Linux computer to perform maintenance and troubleshooting tasks using the vi Editor. The availability and operation of vi Editor is consistent across all UNIX/Linux platforms. Step 1: Log in to Linux as root and start a terminal window click Application > System Tools > Terminal . Step 2: Launch vi by typing “vi studentvi”

The vi Editor is started from the command line. Whenever invoking vi Editor, the user is opening a file. The name of the file to be created or edited can be specified when the user starts vi Editor, a new file can be opened to be named later. It is common to start vi Editor and specify a file name. If the file exists, it is opened for editing. If the file does not exist, it is created.

Page 2: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

2

Review the Three vi Modes There are three modes of operation in vi Editor. Understanding the function of these three modes is the key to working with vi Editor. All commands available with vi Editor can be classified in one of the three modes. Mode Function / Characteristics Command mode Initial default mode for creating and editing files, cursor positioning and modification of existing text. All commands are initiated from this mode.

Insert mode Used for entry of new text. Entering an insert command such as “I” (insert), “a” (append) and “o” (open new line) will take the user from command mode to entry mode. Entry commands are stand-alone and are entered without pressing the Enter key.

Command Mode: Default mode when vi

launch

Page 3: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

3

Last-line mode Used for saving work and quitting vi Editor. Type a colon (:) to get to this mode. Pressing the Enter key or Esc key returns to command mode.

Insert mode> press “esc” + “i”

press “esc” again to exit this mode

Last Line mode: press “esc” + “:”

press “esc” again to exit this mode

Page 4: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

4

Step3: Lets get some hands on Press “esc” + “i” to get to the insert mode. Start typing the following: This is line 1 (press enter) This is line 2 (press enter) This is line 3 (press enter)

Now press “esc” and hit enter to get back to the command mode. You will notice you cannot type any text now except to use the arrow key to go between the 3 lines. In order to continue to insert text, use the arrow key to position where you want the text to be and press “esc” + “a” and start typing. Use the following example:

Press the Esc key to leave Insert Entry mode and return to Command mode. Type “esc” + “:” > a colon to go from Command mode to Last-line mode Type a lower case w (write - to save the new file), followed by a lower case q (quit - to exit the vi Editor) and press Enter You will notice that vi has exited and you are back to the terminal prompt now.

Page 5: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

5

The new file “studentvi” should be saved in the practice directory on the hard disk. Display a long listing of this directory to verify that the file is there (ls –l). Step 4: Open an Existing File Open studentvi, which was created earlier using the command: vi studentvi. You will notice there are already input that were created from the earlier exercise. Position the cursor at the last line “line 5” and press “esc” + “a” to append to the line that was typed earlier. Continue to type line 6 and line 7 as shown below.

Press the Esc key to leave Insert mode and return to Command mode. Step 5: Use Editing Commands There are a many editing commands that can be used to modify existing text in a file. These include commands for deleting, and changing text. To append text. In the command mode use the arrow key to position the cursor and press “esc” + “a”. Delete some text. While in command mode, position the cursor to the desired location and use the options shown in table below to delete some of the mistakes. Note: These commands are ALL lower case and are entered without the Enter key. Basic Text Deletion Commands Command Meaning x Delete character at the cursor dw Delete word (or part of word to right of cursor) 3dw Delete three words dd Delete line containing the cursor 3dd Delete three lines 1. Undo and change some text. To change text and undo changes, use the commands shown in the table below. Many of these commands change the user to Insert mode until Escape is pressed.

Page 6: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

6

Note: These commands are ALL lower case.

Undo and Change Commands Command Meaning u Undo previous command, try to perform a delete and use the “u” to undo the action. Now save and exit by pressing “esc” + “:” then “wq” Step6: Remove Files and Directories Created in this Lab At the prompt, type: rm studentvi verify bu issueing the ls –l command Instructor Initial ___________________

Page 7: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

7

Lab 3b> Managing Run Level in Linux

Objective

The student will understand the purposes that the Runlevels play in a Linux

environment.

Scenario

The system administrator of a large company is responsible for a group of Linux servers whose resources need to be given to the incoming user requests. The systems need to boot up automatically into text mode instead of the GUI mode. The systems resources need to be used for other things besides running the X Window GUI. This task can be accomplished by permanently changing the default runlevel.

Manually change the Runlevel

Manually change the runlevel using the following steps:

1. log in as root 2. Make sure that the system is booted and that the X Window GUI is running. 3. Open a terminal window and type init 3 or /sbin/init 3 at the command prompt. The system should exit the GUI mode and take a few seconds to terminate some running processes. Then, the system will enter text mode.

4. Once the login prompt appears, log back in as the root user.

5. After successfully logged on, type init 5 or /sbin/init 5 at the shell prompt.

6. The GUI login interface will now load. Log in to the X Window session again with the root account.

The runlevel has been successfully changed.

Instructor’s Initial ______________________

Page 8: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

8

Permanently Changing the Runlevel

It is a simple process to permanently change the default runlevel on a Linux system:

7. Open a terminal window, then open the /etc/inittab file with the vi editor. To do this enter vi /etc/inittab at the shell prompt. Read over this file. It contains a lot of useful information regarding the system configuration.

8. Change the default runlevel manually so the system will automatically start in text mode instead of GUI mode. To permanently change the default runlevel, change the id:5:initdefault: line to id:3:initdefault:.

9. Save this edit. To save, press the Esc key on the keyboard, then type :wq and press Enter. This should return the system to a shell prompt.

10. Verify the effect by typing shutdown –r now

11. The Linux system will boot up in text mode. Log in as root and at the prompt, type init 5 to switch back to the GUI mode.

12. Repeat step 7 to 9 but this time change the id:3:initdefault: line to id:5:initdefault:.

13. Verify the effect by repeating step 10. Once system restart, log in as root. Instructor’s Initial ______________________

Page 9: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

9

Lab 3c Shell Script Lab 1

Writing the first shell script

Objective Upon completion of this lab, the student will be able to create a simple shell script file and run it in the Linux environment

Scenario The student is the administrator of a Linux server who wants to create a simple shell script to test whether it is able to run in Linux OS.

Procedures Login into Fedora operating system. Use a text editor such as gedit, vi or emacs. Write required Linux commands and logic in the file. Save and close the file (exit from gedit, vi or emacs). Make the script executable. Test the script by running it, and verify the output.

Step 1: Creating the Script

1. Login into Fedora operating system.

2. Launch gedit Text Editor by clicking on Applications Accessories gedit Text Editor

Page 10: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

10

3. Type in the following using gedit text editor:

#!/bin/bash echo "Hello, World!" echo "Knowledge is power."

4. Save the text by clicking on File Save As…

Page 11: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

11

5. Type in “hello.sh” as the name of the file. Make sure the file is saved in the home directory of the login user account. Close the file after saving.

Step 2: Running the Script

Page 12: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

12

1. Go to terminal by click on Application System Tools Terminal

2. At the terminal, you can run the script by typing as follows:

./hello.sh

Sample outputs:

The command ./hello.sh displayed an error message on the screen. It will not run script since you've not set execute permission for your script hello.sh.

3. Make the script executable by typing as follows:

chmod +x hello.sh

4. To execute this program, type the following command:

./hello.sh

Sample Outputs:

Page 13: Lab 3a Using the vi editor - AT shareatshare.weebly.com/uploads/1/0/8/9/10891690/m7_lab... · Editor is a useful tool when working remotely on other Linux workstations or servers.

M7 Cloud Infrastructure and Operations

13

Reflection:

1) What is one purpose of using shell script?

____________________________________________________________________________

____________________________________________________________________________

2) What is the purpose of using “echo” command?

____________________________________________________________________________

____________________________________________________________________________

3) What is another method to run the script without making it executable? ____________________________________________________________________________ ____________________________________________________________________________


Recommended