You are on page 1of 43

HISTORY OF UNIX

Most discussions of UNIX begin with the history of UNIX without explaining why the
history of UNIX is important to understanding UNIX. The remainder of this document will
describe some strengths and weaknesses of UNIX and attempt to explain why UNIX is becoming
popular. All of UNIX's strengths and weaknesses can be directly related to the history of its
development, hence a discussion of history is very useful.
UNIX was originally developed at Bell Laboratories as a private research project by a
small group of people starting in 1969. This group had experience with a number of different
operating systems research efforts in the 1970's. The goals of the group were to design an
operating system to satisfy the following objectives:
1) Simple and elegant
2) Written in a high level language rather than assembly language
3) Allow re-use of code
Typical vendor operating systems of the time were extremely large and all written in
assembly language. UNIX had a relatively small amount of code written in assembly language
(this is called the kernel) and the remaining code for the operating system was written in a high
level language called C.
The group worked primarily in the high level language in developing the operating system.
As this development continued, small changes were necessary in the kernel and the language to
allow the operating system to be completed. Through this evolution the kernel and associated
software were extended until a complete operating system was written on top of the kernel in the
language C.

UNIX APPLICATION PROGRAMMING INTERFACE

Many proprietary operating systems have a simplified view of application behavior. The
typical application reads some data from disk, tape or a terminal and does some processing.
Output is produced onto disk, tape, tape, terminal, or printer. The operating systems generally
provide easy to use well-implemented facilities to support these types of facilities.
As applications become more sophisticated they need new features such as network access,
multi-tasking, and interprocess communications. In traditional operating systems, these features
are often hard to use, not well documented, and only callable from assembly language. When a
program makes use of these features, the program may be much more complex and much more
difficult to maintain.
In UNIX because the C language was written to be used to implement an operating system
rather than a traditional "input-processing-output" application, use of these sophisticated features
is quite easily done from the C language without writing any assembly language.
In addition, the documentation for these sophisticated features is in the same format and
location as the documentation for the normal application calls.
When UNIX was distributed, users could write applications in C and easily make use of all
of the operating system facilities. This allowed application developers to quickly develop much
more sophisticated applications using these facilities.
The pattern of development in UNIX when adding new features such as networking is to
provide an application program interface from the C language to access the new features.
In general UNIX system developers and application developers program in the same
language using the same application programming interface. In typical proprietary operating
systems, the operating systems programmers are programming in assembly language and have
access to a many capabilities which are not available to the application developer.

UNIX NETWORKING

In 1984, the University of California at Berkeley released version 4.2BSD which included a
complete implementation of the TCP/IP networking protocols. Systems based on this and later
BSD releases provided a multi-vendor networking capability based on Ethernet networking.
The networking support included, remote login, file transfer, electronic mail, and other
important features.
As UNIX was ported onto more and more different types of computer hardware the UNIX
networking allowed many different types of systems to share and mutually use data. Networks
consisting of many different systems could be used as a large distributed system.
When SUN Microsystems added NFS (Network File System), this ability to share and
mutually use data was significantly enhanced.

UNIX POPULARITY

At this point, the reader might be asking, "This document is designed for first time UNIX
users. Why all of this discussion about programming and system programming?" The answer is
because the document is using the history of UNIX to explain why UNIX is so popular. The
application portability and system programming issues have caused many hardware and software
vendors to choose UNIX.
The effect of many vendors choosing UNIX is that there is a wide variety of UNIX systems
available to users at attractive prices.
There are three primary causes for UNIX's popularity (and none is user interface):
Only a very small amount of code in UNIX is written in assembly language. This makes it
relatively easy for a computer vendor to get UNIX running on their system. UNIX is
nearly the unanimous choice of operating system for computer companies started since
1985. The user benefit which results from this is that UNIX runs on a wide variety of
computer systems. Many traditional vendors have made UNIX available on their systems
in addition to their proprietary operating systems.
The application program interface allows many different types of applications to be easily
implemented under UNIX without writing assembly language. These applications are
relatively portable across multiple vendor hardware platforms. Third party software
vendors can save costs by supporting a single UNIX version of their software rather than
four completely different vendor specific versions requiring four times the maintenance.
Vendor-independent networking allows users to easily network multiple systems from many
different vendors.
These features of UNIX have contributed to its rise in popularity since the mid 1980's
USER INTERFACE

So far, there has been no mention of the user interface for UNIX. UNIX is a good
operating system for experienced programmers. The operating system was designed and
implemented by experienced programmers so everything which the experienced programmer
needs is present but not much else. A perfect example of this is the on-line documentation called
"man-pages" or manual pages. The material is completely reference oriented with very little
tutorial information. Experienced programmers find the man pages very useful but the beginning
user often finds them overwhelming.
In the last few years, there has been extensive work to improve the user interface to
UNIX. The most dramatic effort has been the addition of windowing interfaces on top of UNIX
such as X-windows, Suntools, NextStep, Motif, OpenLook, etc. These windowing interfaces do
not change UNIX itself but are built on top of UNIX to provide a more intuitive interface to
UNIX. Each of the different user interfaces has some advantages and some disadvantages.
Currently intensive development effort is being done on all of these Graphical User Interfaces
(GUIs).
Vendors providing UNIX also have done a work to improve the user interface of their
particular versions of UNIX for users without windowing interfaces. Even with all of these
efforts, UNIX is weak in the end-user interface area.

USER PORTABILITY

Even with a relatively poor user interface, UNIX has a following of non-programmer
users. The primary reason for this is because UNIX runs on so many different computer systems
ranging from small desktops to the largest computers in the world. Once a user has learned
UNIX, the skills can be used on many different systems. This ability for a user to work on many
different makes of computer systems without re-training is called "user portability".
Many users of other operating systems have converted to using UNIX because they felt that
UNIX would be the "last" operating system they would have to learn.

OPEN SYSTEMS

There is a recent effort to define what is an "open system" in the international standards
area. An open system is a system which allows application portability, system interoperability,
and user portability between many different computer vendor hardware platforms.




UNIX is a good example of the advantages to the user having an "open system".









BASIC COMMANDS IN UNIX


AIM:
To study the basic commands in Linux.

COMMANDS:

1. TASK : To display the system date and time.
COMMAND : date.
SYNTAX : date.
EXPLANATION : displays the current system date and time on the screen.
OUTPUT : Tue Jun 19 11:37:17 GMT 2007.

2. TASK : To display the current month.
COMMAND : date.
SYNTAX : date +%m.
EXPLANATION : This command displays the current month on the screen.
OUTPUT : 06.

3. TASK : To display the name of the current month.
COMMAND : date.
SYNTAX : date +%h.
EXPLANATION : This command displays the name of the current month on the
screen.
OUTPUT : Jun.

4. TASK : To display the current system date.
COMMAND : date.
SYNTAX : date +%d.
EXPLANATION : This command displays the current system date on the screen.
OUTPUT : 19.



5. TASK : To display the current system date (year).
COMMAND : date.
SYNTAX : date +%y.
EXPLANATION: This command displays the current year on the screen.
OUTPUT : 07.

6. TASK : To display the current system time.
COMMAND : date.
SYNTAX : date +%H.
EXPLANATION: This command displays the current system time (in hours) on the
screen.
OUTPUT : 11.

7. TASK : To display the current system time.
COMMAND : date.
SYNTAX : date +%M.
EXPLANATION: This command displays the current system time (in minutes) on the
screen.
OUTPUT : 43.


8. TASK : To display the current system time.
COMMAND : date.
SYNTAX : date +%S.
EXPLANATION: This command displays the current system time (in seconds) on the
screen.
OUTPUT : 15.







9. TASK : To display the calendar of the current month.
COMMAND : calendar.
SYNTAX : cal.
EXPLANATION: This command displays the calendar of the current month on the
screen.
OUTPUT : Jun 07
S M T W T F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

10. TASK : To display user-defined message.
COMMAND : echo.
SYNTAX : echo message.
EXPLANATION: This command displays on the screen the argument of the echo
command.
OUTPUT : echo OS.


11. TASK : To display the details of all users.
COMMAND : who.
SYNTAX : who.
EXPLANATION: This command lists the information about all the users who have
logged on to that system.
OUTPUT : root :0 2009-02-18 11:26
root pts/0 2009-02-18 11:56 (:0.0)

12. TASK : To display the user detail.
COMMAND : who.
SYNTAX : whoami.
EXPLANATION: This command displays information about the current user of the
system on the screen.
OUTPUT : root.

13. TASK : To create a directory.
COMMAND : make directory.
SYNTAX : mkdir.
EXPLANATION : This command is used to create a new directory with the specified
name.
EXAMPLE : mkdir student.
OUTPUT : The directory student is created.


14. TASK : To change directory.
COMMAND : change directory.
SYNTAX : cd directory name.
EXPLANATION : This command is used to switch from one directory to another.
EXAMPLE : cd staff.
OUTPUT : The directory staff is switched onto.

15. TASK : To delete a directory.
COMMAND : remove directory.
SYNTAX : rmdir directory name
EXPLANATION : This command is used to delete the specified directory.
EXAMPLE : rmdir student.
OUTPUT : The student directory is deleted.

16. TASK : To come out of a sub-directory.
COMMAND : change directory.
SYNTAX : cd ..
EXPLANATION : This command helps in switching to the main directory.
OUTPUT : [root@localhost ~]$ cd ..
[root@localhost home]$ cd ~
[root@localhost ~]$




17. TASK : To list all the files and directories.
COMMAND : list.
SYNTAX : ls.
EXPLANATION : This command displays all the files and directories of the system.
OUTPUT :[root@localhost ~]$ ls
add.c case.sh ex3.c func4.c reverse.c Templates
ADD.C concatefile.sh fact.sh leap.sh storage1.c

18. TASK : To create a file.
COMMAND : cat.
SYNTAX : cat> file name.
EXPLANATION: This command leads to the creation of a new file with the specified
file name and contents.
EXAMPLE : cat> wind.
OUTPUT : A null file called wind is created.

19. TASK : To view a file.
COMMAND : cat.
SYNTAX : cat file name.
EXPLANATION : This command displays the contents of the specified file.
EXAMPLE : cat wind.
OUTPUT : Contents of the file called wind will be displayed on the screen.
20. TASK : To copy a file.
COMMAND : copy.
SYNTAX : cp sourcefile destinationfile.
EXPLANATION : This command produces a copy of the source file and is stored in
the specified destination file by overwriting its previous
contents.
EXAMPLE : cp sun moon.
OUTPUT : The contents of sun file will be copied to the moon file.

21. TASK : To move a file.
COMMAND : move.
SYNTAX : mv sourcefile destinationfile.
EXPLANATION : After moving the contents of the source file into destination file,
the source file is deleted.
EXAMPLE : mv sun moon.
OUTPUT : After copying contents from the sun file to moon file, the
sun file is deleted.

22. TASK : To display / cut a column from a file.
COMMAND : cut.
SYNTAX : cut c no. file name.
EXPLANATION : This command displays the characters of a particular column in the
specified file.
EXAMPLE : cut c3 moon.
OUTPUT : Those characters occurring in the 3
rd
column of the file called
moon are displayed.

23. TASK : To delete a file.
COMMAND : remove.
SYNTAX : rm file name.
EXPLANATION : This command deletes the specified file from the directory.
EXAMPLE : rm sun.
OUTPUT : The file called sun will be deleted.

24. TASK : To retrieve a part of a file.
COMMAND : head.
SYNTAX : head no. of rows file name.
EXPLANATION: This command displays the specified no. of rows form the top
of the specified file.
EXAMPLE : head -1 sun.
OUTPU : The first row of the file called sun is displayed.


25. TASK : To retrieve a file.
COMMAND : tail.
SYNTAX : tail no. of rows file name.
EXPLANATION : This command displays the specified no. of rows form the bottom
of the specified file.
EXAMPLE : tail -1 moon.
OUTPUT : The last row of the file called moon is displayed.

26. TASK : To sort the contents of a file.
COMMAND : sort.
SYNTAX : sort file name.
EXPLANATION : This command helps in sorting the contents of a file in ascending
order.
EXAMPLE : sort win.
OUTPUT : The contents of the file win are displayed on the screen in a
sorted order.

27. TASK : To display the no. of characters in a file.
COMMAND : word count.
SYNTAX : wc file name.
EXPLANATION : This command displays on the screen the no. of rows, words, and
the sum of no. of characters and words.
EXAMPLE : wc ball.
OUTPUT : The no. of rows, words, and no. of characters present in the file
ball are displayed.

28. TASK : To display the calendar of a year.
COMMAND : cal.
SYNTAX : cal year.
EXPLANATION : This command displays on the screen the calendar of the specified
year.
EXAMPLE : cal 2007.
OUTPUT : The calendar of the year 2007 will be displayed.


29. Manual command.
man man This is help command, and will explains you about online manual pages you can also
use man in conjunction with any command to learn more about that command for example.
man ls will explain about the ls command and how you can use it.
man -k pattern command will search for the pattern in given command.

30. Clear command
clear command clears the screen and puts cursor at beginning of first line.

31. Tty command
Tty command will display your terminal. Syntax is
tty options
Options
-l will print the synchronous line number.
-s will return only the codes: 0 (a terminal), 1 (not a terminal), 2 (invalid options) (good
for scripts)

















UNIX: VI EDITOR

General Introduction
The VI editor (short for visual editor) is a screen editor which is available on almost all
Unix systems. Once you have learned vi, you will find that it is a fast and powerful editor. VI has
no menus but instead uses combinations of keystrokes in order to accomplish commands.

Starting VI
To start using VI, at the UNIX prompt type VI followed by a file name. If you wish to edit
an existing file, type in its name; if you are creating a new file, type in the name you wish to give
to the new file.
%vi filename
Then hit Return. You will see a screen similar to the one below which shows blank lines
with tildes and the name and status of the file.
~
~
"myfile" [New file]

VIs Modes and Moods
VI has two modes: the command mode and the insert mode. It is essential that you know
which mode you are in at any given point in time. When you are in command mode, letters of the
keyboard will be interpreted as commands. When you are in insert mode the same letters of the
keyboard will type or edit text. vi always starts out in command mode. When you wish to move
between the two modes, keep these things in mind. You can type i to enter the insert mode. If you
wish to leave insert mode and return to the command mode, hit the ESC key. If you're not sure
where you are, hit ESC a couple of times and that should put you back in command mode.\

General Command Information
As mentioned previously, VI uses letters as commands. It is important to note that in general
vi commands:
are case sensitive - lowercase and uppercase command letters do different things
are not displayed on the screen when you type them
Generally do not require a Return after you type the command.
You will see some commands which start with a colon (:). These commands are ex commands
which are used by the ex editor. ex is the true editor which lies underneath vi -- in other words, vi
is the interface for the ex editor.

Entering Text
To begin entering text in an empty file, you must first change from the command mode to
the insert mode. To do this, type the letter I. When you start typing, anything you type will be
entered into the file. Type a few short lines and hit Return at the end of each of line. Unlike word
processors, vi does not use word wrap. It will break a line at the edge of the screen. If you make a
mistake, you can use the Backspace key to remove your errors. If the Backspace key doesn't work
properly on your system, try using the Ctrl h key combination.

Moving One Character at a Time
Try using your direction keys to move up, down, left and right in your file. Sometimes,
you may find that the direction keys don't work. If that is the case, to move the cursor one
character at the time, you may use the h, j, k, and l keys. These keys move you in the following
directions:
h left one space l right one space
j down one space k up one space
If you move the cursor as far as you can in any direction, you may see a screen flash or hear a
beep.

Moving among Words and Lines
While these four keys (or your direction keys) can move you just about anywhere you
want to go in your file, there are some shortcut keys that you can use to move a little more
quickly through a document. To move more quickly among words, you might use the following:
w moves the cursor forward one word
b moves the cursor backward one word (if in the middle of a
word, b will move you to the beginning of the current word).
e moves to the end of a word.
To build on this further, you can precede these commands with a number for greater
movement. For example, 5w would move you forward five words; 12b would move you
backwards twelve words. [You can also use numbers with the commands mentioned earlier. For
example, 5j would move you down 5 characters.]

Shortcuts
Two short cuts for moving quickly on a line include the $ and the 0 (zero) keys. The $ key
will move you to the end of a line, while the 0 will move you quickly to the beginning of a line.

Screen Movement
To move the cursor to a line within your current screen uses the following keys:
H moves the cursor to the top line of the screen.
M moves the cursor to the middle line of the screen.
L moves the cursor to the last line of the screen.
To scroll through the file and see other screens use:
Ctrl-f scrolls down one screen
Ctrl-b scrolls up one screen
Ctrl-u scrolls up a half a screen
Ctrl-d scrolls down a half a screen

Two other useful commands for moving quickly from one end to the other of a document
are G to move to the end of the file and 1G to move to the beginning of the file. If you precede G
with a number, you can move to a specific line in the document (e.g. 15G would move you to line
15).

Moving by Searching
One method for moving quickly to a particular spot in your file is to search for specific text.
When you are in command mode, type a / followed the text you wish to search for. When you
press Return, the cursor will move to the first incidence of that string of text. You can repeat the
search by typing n or search in a backwards direction by using N.

Basic Editing
To issue editing commands, you must be in command mode. As mentioned before,
commands will be interpreted differently depending upon whether they are issued in lower or
upper case. Also, many of the editing commands can be preceded by a number to indicate a
repetition of the command.

Deleting (or Cutting) Characters, Words, and Lines
To delete a character, first place your cursor on that character. Then, you may use any of the
following commands:
x deletes the character under the cursor.
X deletes the character to the left of your cursor.
dw deletes from the character selected to the end of the word.
dd deletes all the current line.
D deletes from the current character to the end of the line.
Preceding the command with a number will delete multiple characters. For example, 10x
will delete the character selected and the next 9 characters; 10X will delete the 10 characters to
the left of the currently selected character. The command 5dw will delete 5 words, while 4dd
deletes four lines.

Pasting Text using Put
Often, when you delete or cut text, you may wish to reinsert it in another location of the
document. The Put command will paste in the last portion of text that was deleted since deleted
text is stored in a buffer. To use this command, place the cursor where you wish the deleted text
to appear. Then use p to reinsert the text. If you are inserting a line or paragraph use the lower
case p to insert on the line below the cursor or upper case P to place in on the line above the
cursor.

Copying Text with Yank
If you wish to make a duplicate copy of existing text, you may use the yank and put
commands to accomplish this function. Yank copies the selected text into a buffer and holds it
until another yank or deletion occurs. Yank is usually used in combination with a word or line
object such as the ones shown below:
yw copies a word into a buffer (7yw copies 7 words)
yy copies a line into a buffer (3yy will copy 3 lines)
Once the desired text is yanked, place the cursor in the spot in which you wish to insert
the text and then use the put command (p for line below or P for line above) to insert the contents
of the buffer.

Replacing or Changing Characters, Words, and Lines
When you are using the following commands to replace text, you will be put temporarily
into insert mode so that you can change a character, word, line, or paragraph of text.

r replaces the current character with the next character you enter/type.
Once you enter the character you are returned to command mode.
R puts you in overtype mode until you hit ESC which will then return
you to command mode.
cw changes and replaces the current word with text that you type. A dollar
sign marks the end of the text you're changing. Pressing ESC when you
finish will return you to command mode.
Inserting Text
If you wish to insert new text in a line, first position the cursor to the right of where you
wish the inserted text to appear. Type i to get into insert mode and then type in the desired text
(note that the text is inserted before the cursor). Press ESC to return to command mode.

Inserting a Blank Line
To insert a blank line below the line your cursor is currently located on, use the o key and
then hit ESC to return to the command mode. Use O to insert a line above the line the cursor is
located on.
Appending Text
You can use the append command to add text at any place in your file. Append (a) works
very much like Insert (i) except that it insert text after the cursor rather than before it. Append is
probably used most often for adding text to the end of a line. Simply place your cursor where you
wish to append text and press a. Once you've finished appending, press ESC to go back to
command mode.

Joining Lines
Since vi does not use automatic word wrap, it is not unusual in editing lines to end up with
lines that are too short and that might be improved if joined together. To do this, place your cursor
on the first line to be joined and type J. As with other commands, you can precede J with a
number to join multiple lines (4J joins 4 lines).

Undoing
Be sure to remember this command. When you make a mistake you can undo it. DO NOT
move the cursor from the line where you made the change. Then try using one of the following
two commands:

u undoes the last change you made anywhere in the file. Using u again
will "undo the undo".
U undoes all recent changes to the current line. You can not have moved
from the line to recover the original line.
Closing and Saving Files

When you edit a file in vi, you are actually editing a copy of the file rather than the original.
The following sections describe methods you might use when closing a file, quitting vi, or both.
Quitting and Saving a File
The command ZZ (notice that it is in uppercase) will allow you to quit vi and save the edits made
to a file. You will then return to a Unix prompt. Note that you can also use the following
commands:

:w to save your file but not quit vi (this is good to do periodically in
case of machine crash!).
:q to quit if you haven't made any edits.
:wq to quit and save edits (basically the same as ZZ).

Quitting without Saving Edits
Sometimes, when you create a mess (when you first start using vi this is easy to do!) you
may wish to erase all edits made to the file and either start over or quit. To do this, you can
choose from the following two commands:

:e! Reads the original file back in so that you can start over.
:q! Wipes out all edits and allows you to exit from vi.

More about Combining Commands, Objects, and Numbers
Now that you've learned some basic vi commands you might wish to expand your skills by trying
some fancy combination steps. Some commands are generally used in combination with a text
object. We've already seen some examples of this. For example, when you use the command dw
to delete a word, that combines the delete (d) command with the word (w) text object. When you
wish to delete multiple words, you might add a number to this combination. If you wished to
delete 2 words you might use 2dw or d2w. Either of these combinations would work. So, as you
can see, the general format for a command can be
(number) (command) (text object) or (command) (number) (text object)
You might wish to try out some of the following combinations of commands and objects:
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)
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)

Repeating a Command
If you are doing repetitive editing, you may wish to use the same command over and over.
vi will allow you to use the dot (.) to repeat the last basic command you issued. If for example,
you wished to deleted several lines, you could use dd and then . (dot) in quick succession to
delete a few lines.

Useful vi Commands
Cut/Paste Commands:
x delete one character (destructive backspace)
dw delete the current word (Note: ndw deletes n numbered words)
dd delete the current line (Note: ndd deletes n numbered lines)
D delete all content to the right of the cursor
d$ same as above
:u undo last command
p,P paste line starting one line below/above current cursor location
J combine the contents of two lines
"[a-z]nyy yank next n lines into named buffer [a-z]
"[a-z]p/P place the contents of selected buffer below/above the current line

Extensions to the Above Commands:
:3,18d delete lines 3 through 18
16,25m30 move lines 16 through 25 to after line 30
23,29co62 copy specified lines and place after line 62

Cursor Relocation commands:
:[n] goto line [n]
shift g place cursor on last line of text
h/l/j/k move cursor left, right, down and up
^f/^b move forward, backward in text, one page
^u/^d move up, down one half page
$ move to end of line
0 move to beginning of line

Extensions to the Above:
b move backwards one word (Note: nb moves back n number of words)
e move to end of current word
( move to beginning of curent block
) move to the end of current block

Searching and Substitution commands:
/ [string] search forward for string
? [string] search backwards for string
n repeat last search
N repeat search in opposite direction
cw change the contents of the current word, (use ESC to stop
replacement mode)
c$ Replace all content to the right of cursor (exit replacement
mode with ESC)
c0 Replace all content to the left of cursor (exit with ESC)
:1,$s/s1/s2/g (Yow!) global replacement of string1 with string2
r replace current character with next character typed

Entering the Insert Mode:
i Begin inserting text at current cursor location
I Begin inserting text at the beginning of the current line
a Begin appending text, one character to the right of current
cursor location
A Begin appending text at the end of the current line
o/O Begin entering text one line below\above current line
ESC Exit insertion mode and return to command mode



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

Fancy Stuff:
:1,10w file write lines 1 through 10 to file newfile
:340,$w >> file write lines 340 through the end of the file and append
to file newfile
:sh escape temporarily to a shell
^d return from shell to VI
:![command] execute UNIX command without leaving VI
:r![command] read output of command into VI
:r[filename] read filename into VI
:$r newfile read in newfile and attach at the end of current document
:r !sort file read in contents of file after it has been passed through
the UNIX sort
:n open next file (works with wildcard filenames,
ex: vi file*)
:^g list current line number
:set number show line numbers
:set showinsert show flag ("I") at bottom of screen when in insert mode
:set all display current values of VI variables
:set ai set autoindent; after this enter the insert mode and
tab, from this point on VI will indent each line to
this location. Use ESC to stop the indentations.
^T set the autoindent tab one tab stop to the right
^D set the autoindent tab one stop to the left
:set tabstop=n sets default tab space to number n
>> shift contents of line one tab stop to the right
<< shift contents of line one tab stop to the left
























SHELL PROGRAMMING


A Linux shell is a command language interpreter, the primary purpose of which is to
translate the command lines typed at the terminal into system actions. The shell itself is a program,
through which other programs are invoked

What is a shell script?
A shell script is a file containing a list of commands to be executed by the Linux shell. shell
script provides the ability to create your own customized Linux commands
Linux shell have sophisticated programming capabilities which makes shell script powerful
Linux tools
How to work with shell?

Step1:
In the dollar prompt type
$ vi < file name>
Where vi is the editor, it will open a new window in which you can type the program you want

Step2:
After typing the program press ESC and : together then at the bottom of the vi screen you can see
i.e. prompt .In that type as wq which means write and quit i.e. the content what is typed will be
written and saved into that file that has been created

Step3:
Once wq is typed at the: prompt, the prompt would change to $ symbol in which you have to do
the following

$ sh < file name >

Sh command is used to run the shell program
<file name> - is the name of the file for which the output is to be got

Basically to print a text in the your shell programs echo command is used

SYNTAX FOR LOOPING STATEMENTS

IF THEN-ELSE CONSTRUCT

if [ condition]

then <action>
else
statements
fi (end of if)

WHILE
while <condition>
do
<statements>
Done

CASE

Case $<option> in
4) <statements>;;
5) <statements>;;
6) ..
7)
.
.
.
*) <error statement>;;
esac
For loop

For(( intitialization;condition;incremetation/decrementation))

AREA OF RECTANGLE


AIM:
To write a shell script to fine the area of rectangle.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter length and breadth.
STEP 3:Calculate the area using the formula.
STEP 4:Print the area.
STEP 5:Stop the program.

PROGRAM:
[aarthi@localhost ~]$ vi area.sh
echo "ENTER THE length and breadth"
read l b
let area=$l*$b
echo the area is "$area"









RESULT:
Thus a shell script to print the area of rectangle has been written and output is successfully
verified.



TO FIND THE GREATEST AMONG THREE NUMBERS


AIM:
To write a shell script to find the greatest among three numbers.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter the numbers.
STEP 3:Compare the three numbers using if condition.
STEP 4:Print the greatest number.
STEP 5:Stop the program.

PROGRAM:
[aarthi@localhost ~]$ vi big.sh
echo "enter three numbers"
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo "a is big"
elif [ $b -gt $c ]
then
echo " b is big"
else
echo " c is big"
fi
11 22 33
c is big




RESULT:
Thus the shell script has been written and the output is verified successfully.
PRINT NUMBERS USING WHILE LOOP

AIM:
To print natural numbers up to n using while loop.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter the upper limit of the series.
STEP 3:Perform while loop.
STEP 4:Print the series.
STEP 5:Stop the program.

PROGRAM:
[aarthi@localhost ~]$ vi while1.sh
echo enter upper limit
read n
let i=1
while [ $i -lt $n ]
do
echo "$i"
let i=i+1
done










RESULT:
Thus the shell script has been written and the output is verified successfully.
MULTIPLICATION TABLE


AIM:
To write a shell script to display the multiplication table

ALGORITHM:
STEP 1:Start the program.
STEP 2:Assign the values for a and b.
STEP 3:Perform while loop to increase the value of a and b.
STEP 4:Print the values of a,b and u.
STEP 5:Stop the program.

PROGRAM:
[aarthi@localhost ~]$ vi table.sh
// MULTIPLICATION TABLE
let a=1;
let b=1;
while [ $a != 5 ]
do
for b in 1 2 3 4 5 6 7 8 9 10
do
let u=$a*$b
echo $b X $a = $u
let b=$b+1
done
let a=$a+1
echo " "
done

RESULT:
Thus the shell script has been written and the output is verified successfully.
PRINT NATURAL NUMBERS USING FOR LOOP

AIM:
To write a shell script to print the number using for loop

ALGORITHM:
STEP 1:Start the program.
STEP 2:Assign values using for loop.
STEP 3:Print the numbers.
STEP 4:Stop the program.

PROGRAM:
[aarthi@localhost ~]$ vi for.sh
for i in 1 2 3 4 5
do
echo $i
done













RESULT:
Thus the shell script has been written and the output is verified successfully.


FINDING ODD POSITION

AIM:
To write a shell script to find the odd position in a five digit number.


ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter the value of a.
STEP 3:Assign the values for b and c using for loop.
STEP 4:Print b using the if condition of c.
STEP 5:Stop the program.

PROGRAM:
[aarthi@localhost ~]$ vi find.sh
echo enter the number
read a
for i in 1 2 3 4 5
{
let b=$a%10
let c=$i%2
if [ $c != 0 ]
then
echo $b
fi
let a=$a/10
}



RESULT:
Thus a shell script has been written and output is successfully verified.


ARITHMETIC CALCULATOR

AIM:
To write a shell script to design an arithmetic calculator.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Display menu.
STEP 3:Enter the values of a and b.
STEP 4:Enter option code.
STEP 5:Evaluate option code with case constants.
STEP 6:Print the value corresponding to option code.
STEP 7:Stop the program.
PROGRAM:
[aarthi@localhost ~]$ vi case.sh
echo "enter two numbers"
read a b
echo "1.add 2.sub 3.mul 4.div"
read op
case $op in
1) let x=$a+$b
echo $x
;;
2) let x=$a-$b
echo $x
;;
3) let x=$a*$b
echo $x
;;
4) let x=$a/$b
echo $x
;;
5) exit
esac
































RESULT:
Thus the shell script has been written and the output has been verified successfully.

SUM OF DIGITS IN A FIVE DIGIT NUMBER

AIM:
To write a shell script to find the sum of digits of a five digit number.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter a.
STEP 3:Calculate b and sum using for loop of a.
STEP 4:Print the sum using if condition.
STEP 5:Stop the program.

PROGRAM:
[aarthi@localhost ~]$ vi sumofdigits.sh
# Calculates sum of digits of a five digit number
echo Enter a five digit number
read a
for i in 1 2 3 4 5
do
let b=$a%10
let sum=$sum+$b
a=$a/10
if [ $i == 5 ]
then
echo the sum is "$sum"
fi
done




RESULT:
Thus the shell program has been written and the output is verified successfully.

FACTORIAL OF GIVEN NUMBER


AIM:
To write a shell script to find the factorial of a given number.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter the number.
STEP 3:set a loop to find the factorial of the given number using the formula a=a*i.
STEP 4:Print the factorial of the given number.
STEP 5:Stop the program.

PROGRAM:
[aarthi@localhost ~]$ vi fact.sh
echo Enter the number:
read a
let b=$a
let i=$b-1
while [ $i -gt 0 ]
do
let a=$a*$i
let i=$i-1
done
echo the factorial of the number is $a





RESULT:
Thus the shell script has been written and the output is verified successfully.

LEAP YEAR OR NOT

AIM:
To write a shell script to find whether the given year is leap year or not.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter the year.
STEP 3:Calculate the values of x,y,z.
STEP 4:Print the result using if condition.
STEP 5:Stop the program.


PROGRAM:
[aarthi@localhost ~]$ vi leap.sh
echo enter year
read year
let x=$year%100
let y=$year%400
let z=$year%4
if [ $x -eq 0 ]
then
if [ $y -eq 0 ]
then
echo $year is a leap year
else
echo $year is not leap year
fi
else
if [ $z -eq 0 ]
then
echo $year is a leap year
else
echo $year is not a leap year
fi
fi





























RESULT:
Thus the shell script has been written and the output s verified successfully.

STRING CONCATENATION

AIM:
To write a shell script to concatenate two string.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter the strings.
STEP 3:Calculate the length of two strings
STEP 4:Concatenate the two strings.
STEP 5:Print the concatenated string and its length.
STEP 6:Stop the program.

PROGRAM:
[aarthi@localhost ~]$ vi concatstr.sh
echo enter the string
read str1
let a1=${#str1}
echo enter the string
read str2
let a2=${#str2}
let len=$a1+$a2
echo the length of the concatenated string is $len
echo the concatenated string is $str1$str2







RESULT:
Thus the shell script has been written and the output is verified successfully.

FIND THE SUB-STRING IN A STRING


AIM:
To write a shell script to find the position of a sub-string in a string.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter the two strings.
STEP 3:Assign the values of b and c.
STEP 4:Assign the value of d using while and if-else condition
STEP 5:Print the result.
STEP 6:Stop the program.

PROGRAM:
[aarthi@localhost ~]$ vi substr.sh
echo enter the string
read str1
echo enter the substring
read str2
let len1=${#str1}-1
let b=0
let c=0
while [ $c != $len1 ]
do
if [ $str2 == ${str1:$c:2} ]
then
let d=$c
let d=$d+1
echo position matched at $d
break
else
let b=$b+1
fi
let c=$c+1
done
let d=$d+1
if [ $b == $len1 ]
then
echo not match with string
fi
























RESULT:
Thus the shell script has been written and the output has been verified successfully.

CONCATENATION OF FILES


AIM:
To write a shell script to concatenate two files.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter the file name of two files.
STEP 3:Concatenate the files using cat command.
STEP 4:Stop the program
PROGRAM:
[aarthi@localhost ~]$ vi concatefile.sh
echo enter a file
read str1
echo enter second file
read str2
echo $str1
cat $str1>>$str2










RESULT:
Thus the shell script has been written and the output is verified successfully.



STRING EQUALITY


AIM:
To write a shell script to find whether the strings are equal.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter the two strings.
STEP 3:Check for equality of strings using if condition.
STEP 4:Print the result.
STEP 5:Stop the program
PROGRAM:
[aarthi@localhost ~]$ vi string.sh
echo enter string 1
read s1
echo enter string2
read s2
if [ $s1 == $s2 ]
then
echo the given string are equal
else
echo The given strings are not equal
fi





RESULT:
Thus the shell script has been written and the output has been verified successfully.



SWAPPING OF TWO VARIABLES


AIM:
To write a shell script to swap two variable.

ALGORITHM:
STEP 1:Start the program.
STEP 2:Enter the values of a and b.
STEP 3:Process the swaping for the given input.
STEP 4:Print the values of a and b.
STEP 5:Stop the program.

PROGRAM:
[aarthi@localhost ~]$ vi swap.sh
echo Enter any two numbers:
read a
read b
echo The numbers are $a and $b
let c=$a
let a=$b
let b=$c
echo The swapped numbers are $a and $b








RESULT:
Thus the shell script has been written and the output is verified successfully.

You might also like