Pages

Thursday, January 26, 2012

vi Commands

What is vi?

The default editor that comes with the UNIX operating system is called vi (visual editor).


The UNIX vi editor is a full screen editor and has two modes of operation:

Command mode commands which cause action to be taken on the file, and

Insert mode in which entered text is inserted into the file.

In the command mode, every character typed is a command that does something to the text file being edited; a character typed in the command mode may even cause the vi editor to enter the insert mode. In the insert mode, every character typed is added to the text in the file; pressing the (Escape) key turns off the Insert mode.



While there are a number of vi commands, just a handful of these is usually sufficient for beginning vi users. To assist such users, this Web page contains a sampling of basic vi commands. The most basic and useful commands are marked with an asterisk (* or star) in the tables below. With practice, these commands should become automatic.



NOTE: Both UNIX and vi are case-sensitive. Be sure not to use a capital letter in place of a lowercase letter; the results will not be what you expect.





--------------------------------------------------------------------------------





To Get Into and Out Of vi



To Start vi



To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.



* vi filename edit filename starting at line 1

vi -r filename recover filename that was being edited when system crashed







To Exit vi



Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file.



Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is completed by hitting the (or ) key.



* :x quit vi, writing out modified file to file named in original invocation

:wq quit vi, writing out modified file to file named in original invocation

:q quit (or exit) vi

* :q! quit vi even though latest changes have not been saved for this vi call









--------------------------------------------------------------------------------





Moving the Cursor



Unlike many of the PC and MacIntosh editors, the mouse does not move the cursor within the vi editor screen (or window). You must use the the key commands listed below. On some UNIX platforms, the arrow keys may be used as well; however, since vi was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi and should be avoided.



If you go back and forth between a PC environment and a UNIX environment, you may find that this dissimilarity in methods for cursor movement is the most frustrating difference between the two.



In the table below, the symbol ^ before a letter means that the key should be held down while the letter key is pressed.



* j or

[or down-arrow] move cursor down one line

* k [or up-arrow] move cursor up one line

* h or

[or left-arrow] move cursor left one character

* l or

[or right-arrow] move cursor right one character

* 0 (zero) move cursor to start of current line (the one with the cursor)

* $ move cursor to end of current line

w move cursor to beginning of next word

b move cursor back to beginning of preceding word

:0 or 1G move cursor to first line in file

:n or nG move cursor to line n

:$ or G move cursor to last line in file









--------------------------------------------------------------------------------





Screen Manipulation



The following commands allow the vi editor screen (or window) to move up or down several lines and to be refreshed.



^f move forward one screen

^b move backward one screen

^d move down (forward) one half screen

^u move up (back) one half screen

^l redraws the screen

^r redraws the screen, removing deleted lines









--------------------------------------------------------------------------------





Adding, Changing, and Deleting Text



Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use the commands in the following tables.



Perhaps the most important command is the one that allows you to back up and undo your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back more than one step.



* u UNDO WHATEVER YOU JUST DID; a simple toggle







The main purpose of an editor is to create, add, or modify text for a file.



Inserting or Adding Text



The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the key must be pressed to terminate the entry of text and to put the vi editor back into command mode.



* i insert text before cursor, until hit

I insert text at beginning of current line, until hit

* a append text after cursor, until hit

A append text to end of current line, until hit

* o open and put text in a new line below current line, until hit

* O open and put text in a new line above current line, until hit







Changing Text



The following commands allow you to modify text.



* r replace single character under cursor (no needed)

R replace characters, starting with current cursor position, until hit

cw change the current word with new text,

starting with the character under cursor, until hit

cNw change N words beginning with character under cursor, until hit;

e.g., c5w changes 5 words

C change (replace) the characters in the current line, until hit

cc change (replace) the entire current line, stopping when is hit

Ncc or cNc change (replace) the next N lines, starting with the current line,

stopping when is hit







Deleting Text



The following commands allow you to delete text.



* x delete single character under cursor

Nx delete N characters, starting with character under cursor

dw delete the single word beginning with character under cursor

dNw delete N words beginning with character under cursor;

e.g., d5w deletes 5 words

D delete the remainder of the line, starting with current cursor position

* dd delete entire current line

Ndd or dNd delete N lines, beginning with the current line;

e.g., 5dd deletes 5 lines







Cutting and Pasting Text



The following commands allow you to copy and paste text.



yy copy (yank, cut) the current line into the buffer

Nyy or yNy copy (yank, cut) the next N lines, including the current line, into the buffer

p put (paste) the line(s) in the buffer into the text after the current line









--------------------------------------------------------------------------------





Other Commands



Searching Text



A common occurrence in text editing is to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands.



/string search forward for occurrence of string in text

?string search backward for occurrence of string in text

n move to next occurrence of search string

N move to next occurrence of search string in opposite direction







Determining Line Numbers



Being able to determine the line number of the current line or the total number of lines in the file being edited is sometimes useful.



:.= returns line number of current line at bottom of screen

:= returns the total number of lines at bottom of screen

^g provides the current line number, along with the total number of lines,

in the file at the bottom of the screen









--------------------------------------------------------------------------------





Saving and Reading Files

These commands permit you to input and output files other than the named file with which you are currently working.





:r filename read file named filename and insert after current line

(the line with cursor)

:w write current contents to file named in original vi call

:w newfile write current contents to a new file named newfile

:12,35w smallfile write the contents of the lines numbered 12 through 35 to a new file named smallfile

:w! prevfile write current contents over a pre-existing file named prevfile









--------------------------------------------------------------------------------

Tuesday, April 12, 2011

UNDO_RETENTION paramter

Starting in Oracle9i, rollback segments are re-named undo logs. Traditionally transaction undo information was stored in Rollback Segments until a commit or rollback statement was issued, at which point it was made available for overlaying.




Best of all, automatic undo management allows the DBA to specify how long undo information should be retained after commit, preventing "snapshot too old" errors on long running queries.



This is done by setting the UNDO_RETENTION parameter. The default is 900 seconds (5 minutes), and you can set this parameter to guarantee that Oracle keeps undo logs for extended periods of time.



Rather than having to define and manage rollback segments, you can simply define an Undo tablespace and let Oracle take care of the rest. Turning on automatic undo management is easy. All you need to do is create an undo tablespace and set UNDO_MANAGEMENT = AUTO.



However it is worth to tune the following important parameters



The size of the UNDO tablespace

The UNDO_RETENTION parameter

Calculate UNDO_RETENTION for given UNDO Tabespace



You can choose to allocate a specific size for the UNDO tablespace and then set the UNDO_RETENTION parameter to an optimal value according to the UNDO size and the database activity. If your disk space is limited and you do not want to allocate more space than necessary to the UNDO tablespace, this is the way to proceed. The following query will help you to optimize the UNDO_RETENTION parameter:







Because these following queries use the V$UNDOSTAT statistics, run the queries only after the database has been running with UNDO for a significant and representative time!



Actual Undo Size



SELECT SUM(a.bytes) "UNDO_SIZE"

FROM v$datafile a,

v$tablespace b,

dba_tablespaces c

WHERE c.contents = 'UNDO'

AND c.status = 'ONLINE'

AND b.name = c.tablespace_name

AND a.ts# = b.ts#;



UNDO_SIZE

----------

209715200



Undo Blocks per Second



SELECT MAX(undoblks/((end_time-begin_time)*3600*24))

"UNDO_BLOCK_PER_SEC"

FROM v$undostat;



UNDO_BLOCK_PER_SEC

------------------

3.12166667



DB Block Size



SELECT TO_NUMBER(value) "DB_BLOCK_SIZE [KByte]"

FROM v$parameter

WHERE name = 'db_block_size';



DB_BLOCK_SIZE [Byte]

--------------------

4096



Optimal Undo Retention



209'715'200 / (3.12166667 * 4'096) = 16'401 [Sec]



Using Inline Views, you can do all in one query!



SELECT d.undo_size/(1024*1024) "ACTUAL UNDO SIZE [MByte]",

SUBSTR(e.value,1,25) "UNDO RETENTION [Sec]",

ROUND((d.undo_size / (to_number(f.value) *

g.undo_block_per_sec))) "OPTIMAL UNDO RETENTION [Sec]"

FROM (

SELECT SUM(a.bytes) undo_size

FROM v$datafile a,

v$tablespace b,

dba_tablespaces c

WHERE c.contents = 'UNDO'

AND c.status = 'ONLINE'

AND b.name = c.tablespace_name

AND a.ts# = b.ts#

) d,

v$parameter e,

v$parameter f,

(

SELECT MAX(undoblks/((end_time-begin_time)*3600*24))

undo_block_per_sec

FROM v$undostat

) g

WHERE e.name = 'undo_retention'

AND f.name = 'db_block_size'

/



ACTUAL UNDO SIZE [MByte]

------------------------

200



UNDO RETENTION [Sec]

--------------------

10800



OPTIMAL UNDO RETENTION [Sec]

----------------------------

16401



Calculate Needed UNDO Size for given Database Activity



If you are not limited by disk space, then it would be better to choose the UNDO_RETENTION time that is best for you (for FLASHBACK, etc.). Allocate the appropriate size to the UNDO tablespace according to the database activity:







Again, all in one query:



SELECT d.undo_size/(1024*1024) "ACTUAL UNDO SIZE [MByte]",

SUBSTR(e.value,1,25) "UNDO RETENTION [Sec]",

(TO_NUMBER(e.value) * TO_NUMBER(f.value) *

g.undo_block_per_sec) / (1024*1024)

"NEEDED UNDO SIZE [MByte]"

FROM (

SELECT SUM(a.bytes) undo_size

FROM v$datafile a,

v$tablespace b,

dba_tablespaces c

WHERE c.contents = 'UNDO'

AND c.status = 'ONLINE'

AND b.name = c.tablespace_name

AND a.ts# = b.ts#

) d,

v$parameter e,

v$parameter f,

(

SELECT MAX(undoblks/((end_time-begin_time)*3600*24))

undo_block_per_sec

FROM v$undostat

) g

WHERE e.name = 'undo_retention'

AND f.name = 'db_block_size'

/



ACTUAL UNDO SIZE [MByte]

------------------------

200

UNDO RETENTION [Sec]

--------------------

10800

NEEDED UNDO SIZE [MByte]

------------------------

131.695313



The previous query may return a "NEEDED UNDO SIZE" that is less than the "ACTUAL UNDO SIZE". If this is the case, you may be wasting space. You can choose to resize your UNDO tablespace to a lesser value or increase your UNDO_RETENTION parameter to use the additional space.

ORATAB

"oratab" is a file created by Oracle in the /etc or /var/opt/oracle directory when installing database software. Originally ORATAB was used for SQL*Net V1, but lately is's being used to list the databases and software versions installed on a server.




This file may contain comments staring with a pound signs (#) in column one, and data lines consisting of entries in the following format:



database_sid:oracle_home_dir:Y|N



- database_sid is the system id (SID) of an Oracle instances on the server.



- Oracle_home_dir is the ORACLE_HOME directory associated with this instance.



- The Y|N flags indicate if the instance should automatically start at boot time (Y=yes, N=no).



Besides acting as a registry for what databases and software versions are installed on the server, ORATAB is also used for the following purposes:



◦Oracle's "dbstart" and "dbshut" scripts use this file to figure out which instances are to be start up or shut down (using the third field, Y or N).



◦The "oraenv" utility uses ORATAB to set the correct environment variables.





◦One can also write Unix shell scripts that cycle through multiple instances using the information in the oratab file.

Configuring SSH on Cluster Member Nodes

To configure SSH, you must first create RSA and DSA keys on each cluster node, and then copy the keys from all cluster node members into an authorized keys file on each node. Note that the SSH files must be readable only by root and by the oracle user. SSH ignores a private key file if it is accessible by others


To configure SSH, complete the following steps:

Create RSA and DSA keys on each node:

Complete the following steps on each node:

1.Log in as the oracle user.

2.If necessary, create the .ssh directory in the oracle user's home directory and set the correct permissions on it:

$ mkdir ~/.ssh



$ chmod 700 ~/.ssh



$ chmod 700

3.Enter the following commands to generate an RSA key for version 2 of the SSH protocol:

$ /usr/bin/ssh-keygen -t rsa

At the prompts:

◦Accept the default location for the key file.

◦Enter and confirm a pass phrase that is different from the oracle user's password.

This command writes the public key to the ~/.ssh/id_rsa.pub file and the private key to the ~/.ssh/id_rsa file. Never distribute the private key to anyone.

4.Enter the following commands to generate a DSA key for version 2 of the SSH protocol:

$ /usr/bin/ssh-keygen -t dsa

At the prompts:

◦Accept the default location for the key file

◦Enter and confirm a pass phrase that is different from the oracle user's password

This command writes the public key to the ~/.ssh/id_dsa.pub file and the private key to the ~/.ssh/id_dsa file. Never distribute the private key to anyone.

Add keys to an authorized key file:

Complete the following steps:

1.On the local node, determine if you have an authorized key file (~/.ssh/authorized_keys). If the authorized key file already exists, then proceed to step 2. Otherwise, enter the following commands:

$ touch ~/.ssh/authorized_keys



$ cd ~/.ssh



$ ls

You should see the id_dsa.pub and id_rsa.pub keys that you have created.

2.Using SSH, copy the contents of the ~/.ssh/id_rsa.pub and ~/.ssh/id_dsa.pub files to the file ~/.ssh/authorized_keys, and provide the oracle user password as prompted. This process is illustrated in the following syntax example with a two-node cluster, with nodes node1 and node2, where the oracle user path is /home/oracle:

[oracle@node1 .ssh]$ ssh node1 cat /home/oracle/.ssh/id_rsa.pub >> authorized_keys



oracle@node1's password:



[oracle@node1 .ssh]$ ssh node1 cat /home/oracle/.ssh/id_dsa.pub >> authorized_keys



[oracle@node1 .ssh$ ssh node2 cat /home/oracle/.ssh/id_rsa.pub >> authorized_keys



oracle@node2's password:



[oracle@node1 .ssh$ ssh node2 cat /home/oracle/.ssh/id_dsa.pub >>authorized_keys



oracle@node2's password:



Note:



Repeat this process for each node in the cluster.3.Use SCP (Secure Copy) or SFTP (Secure FTP) to copy the authorized_keys file to the oracle user .ssh directory on a remote node. The following example is with SCP, on a node called node2, where the oracle user path is /home/oracle:

[oracle@node1 .ssh]scp authorized_keys node2:/home/oracle/.ssh/

4.Repeat step 2 and 3 for each cluster node member. When you have added keys from each cluster node member to the authorized_keys file on the last node you want to have as a cluster node member, then use SCP to copy the complete authorized_keys file back to each cluster node member

Note:



the oracle user's /.ssh/authorized_keys file on every node must contain the contents from all of the /.ssh/id_rsa.pub and /.ssh/id_dsa.pub files that you generated on all cluster nodes.5.Change the permissions on the oracle user's /.ssh/authorized_keys file on all cluster nodes:

$ chmod 600 ~/.ssh/authorized_keys

At this point, if you use ssh to log in to or run a command on another node, you are prompted for the pass phrase that you specified when you created the DSA key.

Verifying That the User nobody Exists

Before installing the software, complete the following procedure to verify that the user nobody exists on the system:


1.To determine if the user exists, enter the following command:

# id nobody

If this command displays information about the nobody user, then you do not have to create that user.

2.If the nobody user does not exist, then enter the following command to create it:

# /usr/sbin/useradd nobody

Determining if an Oracle Software Owner User Exists

Determining if an Oracle Software Owner User Exists


To determine whether an Oracle software owner user named oracle exists, enter the following command:

# id oracle

If the oracle user exists, then the output from this command is similar to the following:

uid=440(oracle) gid=200(oinstall) groups=201(dba),202(oper)

Oracle Validated Configuration RPM

About the Oracle Validated Configuration RPM


If your Linux distribution is Oracle Linux, or Red Hat Enterprise Linux, and you are an Unbreakable Linux customer, then you can complete most preinstallation configuration tasks by using the Oracle Validated Configurations Setup RPM, available from the Unbreakable Linux Network, or available on the Oracle Linux DVD disks.

When it is installed, the Oracle Validated Configuration RPM does the following:

• Automatically installs any additional packages needed for installing Oracle Grid Infrastructure and Oracle Database

• Creates an oracle user, and creates the oraInventory (oinstall) and OSDBA (dba) groups for that user

• Sets and verifies sysctl.conf settings, system startup parameters, user limits, and driver parameters to values based on recommendations from the Oracle Validated Configurations program

To become an Unbreakable Linux Network customer, contact your sales representative, or purchase a license from the Unbreakable Linux store:

https://shop.oracle.com/product/oraclelinux

To register your server on the Unbreakable Linux Network, or to find out more information, refer to the following URL:

https://linux.oracle.com

If you are using Oracle Linux 4.7 and higher, or Oracle Linux 5.2 and higher, then the Oracle Validated RPM is included on the install media.

Note:

The Oracle Validated RPM sets kernel parameters and resource limits only for the user account oracle. To use multiple software account owners, you must perform system configuration for other accounts manually.

In addition, users and groups are created using the next available ID numbers. If server group and user IDs are not identical on the cluster nodes where you run the Oracle Validated RPM, then it is possible that these ids will be different on the nodes, and the different group and user IDs will cause installation to fail.

To avoid this issue, run the command id user on all nodes, where user is the software owner user account. If group or user IDs are not identical on all the nodes, then change them as necessary to ensure that they are identical.



Installing the Oracle Validated RPM From Unbreakable Linux Network

Use the following procedure to subscribe to Oracle Unbreakable Linux channels, and to add the Oracle Linux channel that distributes the Oracle Validated Configurations Setup RPM:

1. Complete a default Oracle Linux workstation installation, or a default Red Hat Enterprise Linux installation.

2. Register your server with Unbreakable Linux Network (ULN). By default, you are registered for the Oracle Linux Latest channel for your operating system and hardware.

3. Log in to Unbreakable Linux Network at the following URL:

https://linux.oracle.com

4. Click the Systems tab, and in the System Profiles list, select a registered server. The System Details window opens, and displays the subscriptions for the server.

5. From the Available Channels list, select the _base and _patch channels corresponding to your Oracle Linux distribution. For example, if your distribution is Oracle Linux 5 Update 5 for x86_64, then select the following:

o Oracle Linux 5 Update 5 installation media copy (x86_64)

o Oracle Linux 5 Update 5 Patch (x86_64)

6. Click Subscribe.

7. From a terminal session, as root, enter the following command:

8. # up2date --nox --show-channels

You should see output indicating that you have subscribed to the Oracle Linux channel. For example:

el5_u5_i386_base



el5_u5_x86_64_patch

9. Open a terminal session as root, and install the Oracle Validated Configurations Setup RPM with up2date, using the following command:

10. # up2date --install oracle-validated

Oracle Linux automatically creates a standard (not role-allocated) Oracle installation owner and groups, and sets up other kernel configuration settings as required for Oracle installations.

11. Repeat steps 1 through 8 on all other servers in your cluster.

Note:

Check the Oracle Validated Configuration RPM log file to review system configuration changes:

/var/log/oracle-validated/results/orakernel.log



Installing the Oracle Validated RPM From DVD Disks or Images

Use the following procedure to install the Oracle Validated Configuration RPM from the Oracle Linux distribution:

1. Obtain Oracle Linux disks either by ordering the Oracle Linux Media Pack from Oracle Store, or downloading disk images from the Oracle E-Delivery Web site for Oracle Linux and Oracle VM.

Oracle Store:

https://shop.oracle.com/store/enterpriselinux

E-Delivery Web Site:

http://edelivery.oracle.com/linux



Balan Binoy Kumar