Sem 3‎ > ‎OS (Unix) LAB‎ > ‎

P0: Getting Started w/ Shell Programming.

posted Oct 8, 2011, 10:59 PM by Neil Mathew   [ updated Oct 9, 2011, 3:36 AM ]

Follow the simple steps below:

Step 1: Telnet <IP of linux based server to connect to> for me. for Amity Labs.

Step 2: Command: vi prog_name
OR vi

Step 3: Editor Opens, Type in program.

Step 4: Once done, press ESC

Step 5: Type :wq (Save and Quit)

Step 6: To run program, Command: sh prog_name
Backspace doesn't work, use DEL which deletes backwards.

The above are simple steps to make the program and run it.
Below is a more elaborate understanding of each step:

Vi Editor

Writing the program is not as simple as it may seem.  

There are two sides of input on the vi editor switchable using the ESC and a /button.

One (Insert Mode)
receives keyboard input as it is to type in and complete the program.
Left & Right arrow keys work fine, but the up and down arrow keys leave an unwanted character behind. 

The other(Command Mode),
is basically another command line to make edits on inputted lines, like
delete line: d + d (double click d)
delete character: d + (space


     vi filename             edit a file named "filename"
     vi newfile              create a new file named "newfile"


i               Begin inserting text at current cursor location 
        a		Begin appending text, one character to the right of current
	        	cursor location

I		Begin inserting text at the beginning of the current line

A		Begin appending text at the end of the current line
R              Begins replacing text from cursor location

o		Begin entering text one line below current line
O              Begin entering text one line above current line
ESC		Exit insertion mode and return to command mode


ZZ save file and exit VI
:wq		        same as above
:e!		        return to last saved version of current file
:q	        	quit without save, 
:q! is required if changes have been made
:w		        write without exit 
:w! to force write


     h                    Move cursor left one space

     j                     Move cursor down one line
     k                    Move cursor up one line
     l                    Move cursor right one space
    <Arrow Keys>    These work too 

     w                   forward word by word
     b                    backward word by word
     e                    move forward to the end of current word

     $                    to end of line

     0 (zero)           to beginning of line

     H                    to top line of screen

     M                    to middle line of screen

     L                     to last line of screen

     G                     to last line of file
     nG                   to nth line of file
1G        to first line of file

     <Control> + f        scroll forward one screen

     <Control> + b       scroll backward one screen
     <Control> + d       scroll down one-half screen

     <Control> + u       scroll up one-half screen


(Copy & Delete/Cut & Paste)            

x     delete/cut one character (destructive backspace) d + (space)    Same as above
dw		    delete/cut the current word 
                         ndw deletes following (n-1) wprds including current (1)
dd 		    delete/cut the current line
                         ndd deletes following (n-1) lines incl current (1)
D		    delete/cut all content to the right of the cursor
d$		    same as above

y + (space)    Copy current character
yy                Copy or "yank" the current line
                         nyy copies following (n-1) lines incl current (1)

p		    paste what was cut last
p/P		    pasting a line below/above current line

c + (space)     deletes current character and opens insert mode at spot
                    (basically CHANGE COMMAND)
r                   same as above but known as REPLACE
s                   same as above but known as SUBSTITUTE
C                  deletes all from cursor to end of line, enters insert mode
S                  deletes the WHOLE line, then enters insert mode.
R                   All input from cursor point replaces previous text
J                   Joins next line with the current line (removes newline)
                            nJ joins following n-1 lines to current line (1)

(Undo & Redo)     

u		    undo last (command mode) edit
.		                  redo last


/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

         move to next occurrence of search string in opposite direction

(Line Number)

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

:=         returns the total number of lines at bottom of screen
nG             move to nth line of file


Command                                 Text Object

d (delete)	                        w (word to the left)
y (yank/copy)                	b (word to the right or backward)
c (change)	                        e (end of word)
                                        <space> (current character)
		                        H (top of the screen)
		                        L (bottom of the screen)
		                        M (middle of the screen)

		                        0 (zero - first character on a line)
		                        $ (end of a line)

		                        ( (previous sentence)
		                        ) (next sentence)
		                        [ (previous section)
		                        ] (next section)