Está en la página 1de 41

1

Introduction
Linux refers to the family of Unix-like computer operating systems using the Linux kernel. Linux can be installed on a wide variety of computer hardware, ranging from mobile phones, tablet computers and video game consoles, to mainframes and supercomputers Linux is a leading server operating system, and runs the 10 fastest supercomputers in the world. The development of Linux is one of the most prominent examples of free and open source software collaboration; typically all the underlying source code can be used, freely modified, and redistributed, both commercially and non-commercially, by anyone under licenses such as the GNU General Public License. Typically Linux is packaged in a format known as a Linux distribution for desktop and server use. Some popular mainstream Linux distributions include Debian (and its derivatives such as Ubuntu), Fedora and opens USE. Linux distributions include the Linux kernel and supporting utilities and libraries to fulfill the distribution's intended use. Linux is a true 32-bit operating system that runs on a variety of different platforms, including Intel, Sparc, Alpha, and Power-PC (on some of these platforms, such as Alpha, Linux is actually 64-bit). There are other ports available as well, but I do not have any experience with them.

There are two ways to install Linux:Graphical mode :- If you have installed Linux before you can choose
either. The graphical install is better for you if you are a new user because you are guided through every step with help in the side pane.

Text mode: The text-based install allows you more control over what is going
on during the install. This, however, requires some experience with installing Linux

History of Linux
Linux is the first truly free Unix-like operating system. The underlying GNU Project was launched in 1983 by Richard Stallman originally to develop a Unixcompatible operating system called GNU, intended to be entirely free software. Many programs and utilities were contributed by developers around the world, and by 1991 most of the components of the system were ready. Still missing was the kernel. Linus Torvalds invented Linux itself. In 1991, Torvalds was a student at the University of Helsinki in Finland where he had been using Minix, a non-free Unixlike system, and began writing his own kernel. He started by developing device drivers and hard-drive access, and by September had a basic design that he called Version 0.01. This kernel, which is called Linux, was afterwards combined with the GNU system to produce a complete free operating system. On October 5th, 1991, Torvalds sent a posting to the comp.os.minix newsgroup announcing the release of Version 0.02, a basic version that still needed Minix to operate, but which attracted considerable interest nevertheless. The kernel was then rapidly improved by Torvalds and a growing number of volunteers communicating over the Internet, and by December 19th a functional, stand-alone Unix-like Linux system was released as Version 0.11. On January 5, 1992, Linux Version 0.12 was released, an improved, stable kernel. The next release was called Version 0.95, to reflect the fact that it was becoming a full-featured system. After that Linux became an underground phenomenon, with a growing group of distributed programmers that continue to debug, develop, and enhance the source code baseline to this day. Linux continued to be improved through the 1990's, and started to be used in large-scale applications like web hosting, networking, and database serving, proving ready for production use. Version 2.2, a major update to the Linux kernel, was officially released in January 1999. By the year 2000, most computer companies supported Linux in one way or another, recognizing a common standard that could finally reunify the fractured world of the Unix Wars. The next major release was V2.4 in January 2001, providing (among other improvements) compatibility with the upcoming generations of Intel's 64-bit Itanium computer processors.

Common Linux Features


1.Low cost:You dont need to spend time and money to obtain licenses since Linux and much of its software come with the GNU General Public License. You can start to work immediately without worrying that your software may stop working anytime because the free trial version expires. Additionally, there are large repositories from which you can freely download high quality software for almost any task you can think of.

2.Stability:Linux doesnt need to be rebooted periodically to maintain performance levels. It doesnt freeze up or slow down over time due to memory leaks and such. Continuous up-times of hundreds of days (up to a year or more) are not uncommon.

3. Performance:Linux provides persistent high performance on workstations and on networks. It can handle unusually large numbers of users simultaneously, and can make old computers sufficiently responsive to be useful again.

4.Network friendliness:Linux was developed by a group of programmers over the Internet and has therefore strong support for network functionality; client and server systems can be easily set up on any computer running Linux. It can perform tasks such as network backups faster and more reliably than alternative systems.

5.Flexibility:Linux can be used for high performance server applications, desktop applications, and embedded systems. You can save disk space by only installing the components

needed for a particular use. You can restrict the use of specific computers by installing for example only selected office applications instead of the whole suite.

7.Choice:The large number of Linux distributions gives you a choice. Each distribution is developed and supported by a different organization. You can pick the one you like best; the core functionalities are the same; most software runs on most distributions.

8.Fast and easy installation:Most Linux distributions come with user-friendly installation and setup programs. Popular Linux distributions come with tools that make installation of additional software very user friendly as well.

9.Full use of hard disk:Linux continues work well even when the hard disk is almost full.

10.Multitaskin:Linux is designed to do many things at the same time; e.g., a large printing job in the background wont slow down your other work.

11.Security:Linux is one of the most secure operating systems. Walls and flexible file access permission systems prevent access by unwanted visitors or viruses. Linux users have to option to select and safely download software, free of charge, from online repositories containing thousands of high quality packages. No purchase transactions requiring credit card numbers or other sensitive personal information are necessary.

12.Open Source:- If you develop software that requires knowledge or


modification of the operating system code, Linuxs source code is at your fingertips. Most Linux applications are Open Source as well.
5

Installing Linux
Choosing an Installation Method
There are a number of different ways in which you can install your system. They are all documented further later in the chapter. They are as follows: CD-ROM: This method needs a boot floppy, bootable CD, or a PCMCIA boot disk. This is the most common way to install your system. Hard Drive: This method requires you to have the Red Hat installation files on your hard drive. To install Red Hat Linux from a hard drive, you need the same startup and supplemental disks used by the FTP install. You must first create a Red Hat directory called RedHat at the top level of your directory tree. Everything you install should be placed in that directory. Copy the base subdirectory and then copy the packages you want to install to another subdirectory called RPMS (Red Hat Package Manager). Basically, these are your program installation files. You can use available space on an existing DOS partition or a Linux partition that is not required in the install procedure (for example, a partition that would be used for data storage on the installed system). With Red Hat 7 being a 3-CD install, there is going to be the overhead of having enough space free to store these files.If you are using a DOS filesystem, you might not be able to use the full Linux filenames for the RPM (Red Hat Package Manager) packages. The installation process does not care what the filesystem looks like, but it is a good idea to keep track of it so you will know what you are installing. NFS (Network File System) Image: You can install from an NFS server that has the install files exported. This requires a network or PCMCIA boot disk. You should either have nameservices configured or know the NFS server's IP address and the path to the exported CD-ROM. This method is only advisable if the NFS server is on a local network. It will take a long time to install using this, the FTP, and the HTTP install method. There is the possibility of the connection timing out. FTP (File Transfer Protocol) and HTTP: Comparable to an NFS image install. This also requires a network or PCMCIA boot disk. You must have a startup disk and a supplemental disk for an FTP install. You need to have a valid
6

nameserver configured or the IP address of the FTP or HTTP server you will be using. You also need the root path of the Red Hat Linux directory on the site.

Setting up Hard Drive


Linux looks at hard drives differently than DOS/Windows. Instead of calling the first partition C:, the second one D:, and so on, it will usually be a combination of characters that signify the BUS and hard drive number. It works like this:
had: First IDE controller, Master hdb: First IDE controller, Slave hdc: Second IDE controller, Master hdd: Second IDE controller, Slave sda: First SCSI drive on the SCSI BUS

This is only the name of the devices as Linux sees them. The actual way Linux views your partitions is
hda1: Primary partition on first hard drive. hda5: Extended partition on first hard drive. The extended partition will

always have the number 5 as its partition number.

Understanding the Swap Space


A swap space is a space on a hard disk used as the virtual memory extension of a computer's real memory (RAM). Having a swap space allows your computer's operating system to pretend that you have more RAM than you actually do. The least recently used files in RAM can be "swapped out" to your hard disk until they are needed later so that new files can be "swapped in" to RAM. When you create your swap partition, give some extra thought to the size of this partition (versus blindly picking a number or taking another person's recommendation). You should have at least 16MB total between your RAM and swap space. If you are running X, you should have at least 32MB between them. X
7

is a hog on RAM and will generally use up to 50% of your RAM, so it makes sense to have a decent sized swap file. A better way to estimate how much RAM you need is to figure out the size of all the programs you would run at one time. To this number, add 24MB to cover the OS. If the total is less than 64MB, use a 64MB swap space; otherwise, use the actual value. These days, with most machines coming with 64MB128MB of RAM a swap space of 80MB100MB is about right. You should always configure some swap space, regardless of how much RAM you have. Even a small amount of swap space will have good results on a system with a lot of RAM. For example, I have 256MB of RAM on my system, which is more than enough for all of the programs I run. I have 127MB of swap space for programs I have running but am not actively using. To create your swap partition using fdisk, you need to use n for a new partition. Choose either primary or extended; you most likely need primary. Give the partition a number and tell it where the first cylinder should be. Last, tell fdisk how big you want your swap partition. Now you need to change the partition type to Linux swap. Enter t to change the type and enter the partition number of your swap partition. Enter 82 for the hex code for the Linux swap partition. You have created your Linux and Linux swap partitions, and it is time to add any partitions you might need (for example, Windows 95). Use n again to create a new partition and enter all the information just as before. However, after you enter the size of the partition, you need to change the partition type. Enter l to get a listing of the hex codes for the different partition types. Find the type of partition you need and use t to change the partition type. Repeat this procedure until all your partitions are created. You can create up to four primary partitions; then you must start putting extended partitions into each primary partition. After your partitions are created, the installation program looks for Linux swap partitions and asks to initialize them. Choose the swap partitions you want to initialize, select the Check for Bad Blocks During Format box, and click OK. This formats the partition and makes it active so Linux can use it.

Commands of Linux
In Linux, the commands are interpreted by a command-line interface called shell. Shell provides a user interface that responds to particular commands, programs or scripts that a user creates. The default Red Hat Linux shell is known as Bourne Again Shell or bash. The Linux shell has various shell commands to run programs, manage Linux files and interact with the computers hardware.

File Commands:File commands are used to remove, rename or move files in the directory structure. You can also display the contents of a specified file using these commands. The file commands create new files by copying the contents of one file into another file. The various file commands are:

cat > command:-The cat > command is also used for creating a file. You
should now know how to create small files. Enter the command cat, followed by the > (the right chevron) character and the filename. Ctrl+Z command is used to Save the contents of the existing file. Its Syntax is:[root@linux Msc(IT)(LE)]# cat > filename For ex--[aman@linux Msc(IT)]# cat > deep I am the student of Msc IT class. ^z [1]+ stopped cat > deep These are options of cat command:-E, --show:-ends display $ at end of each line. -n, --number:- number all output lines. -T, --show-tabs:- display TAB characters as ^I

cp command:-The cp (copy) command copies a files and directories. The


syntax requires at least two filenames to be specified in the command line. When both are ordinary files, the first is copied to the second. Its syntax is:[root@linux Msc(IT)]# cp firstfilename secondfilename
9

For ex[aman@linux Msc(IT)]# cp deep randeep [aman@linux Msc(IT)]# These are the options of cp command:-l, --link:link files instead of copying -Z, --context=CONTEXT:- set security context of copy to CONTEXT

rm command:-The rw (remove) command remove or delete files from the


current working directory. Its syntax is :[root@linux Msc]# rm filename For ex--[aman@linux Msc(IT)(LE)]#cat Aman Raman Anu Randeep [aman@linux Msc(IT)]#rm Raman [aman@linux Msc(IT)]#cat Aman Anu Randeep These are the optios of rm command:-f, --force:- ignore nonexistent files, never prompt -i --prompt before every removal

mv command:-The mv (move) command moves a file or directory from one


directory to another directory. It has two functionsrenaming a file (or directory) and moving a group of files to a different directory. Its syntax is:[root@linux Msc]# mv filename dirname For ex[aman@linux Msc]# mv Aman deep [aman@linux Msc(IT)]# These are the options of mv command:mv oldfile newfile will rename oldfile to newfile. mv -i oldfile newfile for confirmation prompt. mv -f oldfile newfile will force the rename even if target file exists. mv * /usr/bajwa/ will move all the files in current directory to /usr/bajwa directory.

10

mv .. / command:-The mv .. / command moves a Parent directory files to


Child directories. Its syntax is:[root@linux Msc(IT)(LE)]# mv ../ Pfilename Cdirname For ex[aman@linux Msc(IT)]# tree deep(file) randeep(file) Sunny(dir) anu(file) kamal (dir) guri (file) But I want to move the Parent file randeep to Child directory kamal . So this command is used. [aman@linux sunny ]# mv . . / randeep kamal [aman@linux sunny ]#

more command:-If a file is too large for its contents to fit in one screen, it will
scroll off your screen when you view it with cat command, but more command views the contents of a file, Page Wise. The more command displays the first page of the contents of a file existing in the current working directory. Its syntax is:[root@linux Msc(IT)(LE)]# more filename For ex--[aman@linux Msc(IT)(LE)]# more sunny [aman@linux Msc(IT)(LE)]# These are the options of the more command:-p Do not scroll. Instead, clear the whole screen and then display the text. -p Do not scroll. Instead, clear the whole screen and then display the t-num.This option specifies an integer which is the screen size.

wc command:- The wc (word count) command is used to counts lines, words


and characters in files. It takes one or more filenames as its arguments, and displays a four-columnar output. First column show the lines no, second word no, third character no, and forth is display the file name. Its syntax is:[root@linux Msc]# wc filename
11

For ex--[aman@linux Msc(IT)]# wc randeep [aman@linux Msc(IT)]# These are the options of wc command:wc -l filename will print total number of lines in a file. wc -w filename will print total number of words in a file. wc -c filename will print total number of characters in a file.

cmp command:-The cmp (compare) command is used to Compare the two files.
Obviously, it needs two filenames as arguments. Its syntax is:[root@linux Msc(IT)(LE)]# cmp firstfilename secondfilename For ex--[randeep@linux Msc(IT)(LE)]# cmp aman sunny These are the options of cmp command:-l :- Print the byte number (decimal) and the differing byte values (octal) for each difference. -s :- Print nothing for differing files; return exit status only.

comm command:-This command is used to Finding what is COMMON in two


files. [root@linux Msc(IT)(LE)]# comm firstfilename secondfilename For ex--[aman@linux Msc(IT)(LE)]# comm sunny aman These are the options of comm. Command:--check-order:-check that the input is correctly sorted,even if all input lines are pairable. --no check-order:-do not check that the input is correctly sorted.

12

Directory commands:In Linux, directory commands are used to create and delete directories in a file system. You can also display the contents of a directory using these commands. The contents of a directory are the files and sub-directories stored in a particular directory. The various directory commands are:

Clear command:-The clear command is used to clear the screen.


Its Syntax is:[root@linux root]# clear OR [root@linux root]# tput clear

ls command:-This command is used to displays the contents of a files and


directory. Its Syntax is:[root@linux root]# ls DIR: AMAN ABC FILES: ANU MAAN RANDEEP Options: ls will list all the files in your home directory, this command has many options. ls -l will list all the file names, permissions, group, etc in long format. ls -a will list all the files including hidden files that start with . . ls -lt will list all files names based on the time of creation, newer files bring first. ls -Fxwill list files and directory names will be followed by slash. ls -Rwill lists all the files and files in the all the directories, recursively. ls -R | more will list all the files and files in all the directories, one page at a time.

SIDHU ASM ASM2 DEEP

SSSS1

13

mkdir command:-The mkdir command creates new directories.


syntax of mkdir command:[root@linux root]# mkdir dirname For ex[randeep@linux root]# mkdir Msc(IT) [randeep@linux root] # ls Maan Sidhu Malhi Anu [randeep@linux root]# These are options of mkdir command:-m, --mode=MODE- set file mode (as in chmod), not a=rwx umask . v, --verbose- print a message for each created directory.

cd / command:-The cd / command is used to open the directory.


Its Syntax is:[root@linux root]# cd / dirname For ex[aman@linux root]# cd / ASM [aman@linux ASM]#

cd . . command:-The cd . . command is used to close the open directory. Its


Syntax is:[root@linux Sidhu]# cd . . For ex[aman@linux Sidhu]# cd . . [aman@linux root]#

rmdir command:-The rmdir command removes or deletes the existing


directories. Its Syntax is:[root@linux root]# rmdir dirname For ex[aman@linux root]# ls RANDEEP ABC AMAN GGG [aman@linux root]# rmdir RANDEEP [aman@linux root]# ls ABC AMAN GGG
14

Options: rm -r directory_name will remove all files even if directory is not empty. rmdir parminder is how you use it to remove parminder directory. rmdir -p will remove directories and any parent directories that are empty. rmdir -s will suppress standard error messages caused by -p.

Other commands of Linux:chmod command:-Theres an important default security feature provided by


LINUX by chmode command. The chmod command is used to set the three permissions for all three categories of users (owner, group, and others) of a file.It can be used only by the owner of the file. Its syntax is:[root@linux Msc(IT)(LE)]# chmod category operation permission filename For ex[aman@linux Msc(IT)LE)]# chmod u+w sunny These are the options of the chmod command:-B:-change file and directories recursively. -v:-output a diagnostic for every file processd. -C:-like verbose but report only when a change is made. Table: Abbreviations Used by chmod Operation Permission +--assign permission rread permission - --remove permission wwrite permission =--assign absolute permission xexecute

Category uuser ggroup oother permission aall

15

cal command:-With the cal (CALENDAR) command, you printed the calendar
for a month on the Screen. Any calendar from the year 1 to 9999 can be displayed with this command. Its syntax is:[root@linux root]# cal yearname For ex--[root@linux Msc]# cal -1 March 2011 Su Mo Tu We Th Fr Sa 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 31 who command:-Linux maintains an account of all the current users of the system. Its a good idea to know the people working on the various terminals so that you can send them messages directly. Its syntax is:[root@linux root]# who These are the options of who commands:-b, --boot: time of last system boot -m :- only hostname and user associated with stdin -q, --count:- all login names and number of users logged on su command:-The su command facilitates you to switch b/w user accounts. You can change the control from a user account connected to the shell prompt to another user account by using a login ID and password of the user. Its syntax is:[root@linux root]# su newusername For ex--[root@linux root]# su sandeep These are the options of su command:-l, --login:- make the shell a login shell -c, --command=COMMAND:- to the pass a single COMMAND shell with -c
16

passwd command:-The passwd command is used to change passwords.


Its syntax is:[root@linux root]# passwd newpassword These are the options of passwd command:-d:-This is a quick way to delete a password for an account. -x:-This will set the maximum password lifetime. -s:-This will output a short information about the status of the password for a given account. echo command:-The echo command is used to DISPLAY MESSAGES on the screen. This command was used with both quoted arguments. Its syntax is: [root@linux root]# echo Show any arguments For ex--[aman@linux Msc(IT)]# echo Hello, hw r u dear pwd command:-The pwd (present working directory) command is used to CHECK THE CURRENT WORKING DIRECTORY. Its syntax is: [root@linux root]# pwd /home/aman These are the options of pwd command:-L:-use pwd from environment,even if it contain symlinks. -P:-avoid all symlinks. grep command:-The grep command is used to SEARCHING FOR A PATTERN in File. It scans a file for the present of a pattern, and can display the selected pattern, the line numbers in which they were found, or the filenames where the pattern presents. grep can also select lines not containing the pattern. Its syntax is:[root@linux Msc(IT)(LE)]# grep pattern filename For ex--[aman@linux E)]# grep sunny

17

These are the options:-b option will precede each line with its block number. -c option will only print the count of matched lines. -i ignores uppercase and lowercase distinctions. -l lists filenames but not matched lines.

ps command:-The ps command is used to display the attributes of a process. By


default, ps lists out the processes associated with a user at the terminal. Its syntax is:[root@linux root]# ps These are the options of the ps command:-A:-select all process Identical to e. -d:-select all process expect session leaders. -e:-select all process Identical to-A kill command:-The kill command is used to killed the any process. Its syntax is:[root@linux Msc(IT)(LE)]# kill process id For ex--[aman@linux Msc(IT)(LE)]# kill 8109 These are the options of kill command:-l:-print a of single names. -p:-specify that kill should only print the procees id of the named process,and not send any signals. df command:-The df command reports the amount of free space available on the disk. The output always reports for each file system separately. Its syntax is:[root@linux Msc(IT)(LE)]# df These are the options df command:-a:- include dummy file system.
18

-B:-use size-byte blocks. -H:-likewise,but powers of 1000 not 1024. free command:-The free command is used to display the all free space in system. Its syntax is:[root@linux Msc(IT)(LE)]# free These are the options of free command:-b:-It switch displays the amount of memory in bytes. -t:-It switch displays aline containing the total. -l:-It switch shows detailed low and high memory statistics.

du command:-The du (Disk usage) command reports usage by a recursive


examination of the directory tree. By default, du lists the usage of each subdirectory of its argument, and finally produces a summary. Its system is:[root@linux Msc(IT)(LE)]# du These are the options of du command:-a:-write counts for all files , not just directories. -B:-use size byte blocks. -c:-produce a grand total.

find command:-The find command is used to Search for files in a directory


hierarchy. Its syntax is:[root@linuxMsc(IT)(LE)]# find filename, findfilename These are the options of find command:a:-The acess time of the file reference. B:-The birth time of the file reference. c:-The node status change time of reference.
19

history command:-The history command is used to display the list of


commands most recently used, we can easily find out the command recently used. Its syntax is:[root@linux Msc(IT)(LE)]# histoty

man command:-The man command is used to display all information about


any command. Its syntax:[root@linux Msc(IT)(LE)]# man These are the options of man command: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.

hostname command:-The hostname command is used to display the system


hostname. Its syntax is:[root@linux Msc(IT)(LE)]# hostname These are the options of hostname command:-a:-display the alias name of the host. -d:-display the name of dns domain. -i:-display the IP address of the host. id command:-The id command is used to display the current id on the system. Its syntax is:[root@linux Msc(IT)(LE)]# id These are the options of id command :-g:-print all group ids. -n:-print a name instead of number,for ugc. -u:-print only the effective user id. mount command:-The mount command is used to mount the various hardware devices such as CD-ROM and floppy drive. Its syntax is:[root@linux Msc(IT)(LE)]# mount / dev/cdrom dirname

20

These are option of mount command:-h:-print help message. -v:-print a version of string. umount command:-The umount command is used to close the various hardware devices such as CD-ROM and floppy drive. Its syntax is:[root@linux Msc(IT)(LE)]# umount / dev/cdrom

Wild card entry Command:These command are used for wild card entries. i) ? - This is used for the read the single character from the file name or extension of the file. Its syntax is: [root@linux root]# ls Sunny?.txt This command catch all the txt files those starts from such and have 4 previous character. *- This command used for read all the character and extension of files. Its syntax is : [root@linux Msc(IT)(LE)]# ls *.* This shows the list of all files presents in the current directory.

ii)

poweroff command:-The poweroff command is used to prematurely terminate


Linux operating System. Its syntax is:[root@linux Msc(IT)(LE)]# poweroff These are the options of poweroff command:-f:-does not invoke shutdown and instead performs the actual action you would expect from the name. -p:-Instructs the halt command to instead behave as poweroff. -i:- Iterates configured network interfaces and brings them down before shutting down.

21

22

Vi editor
All Linux configuration files are written in plain English, easy to read and to adapt. You use a text-editor to write or make changes to such files. The two most popular, powerful, and unfortunately "difficult" text editors, both of which are found in every Linux distro, are Vi and Emacs. Most GUI-based editors, such as Kedit, are easier to manage. But don't make the mistake of thinking that a GUI-based editor is all you need. There are situations that crop up with Linux that require a text-mode editor -- in other words, when you don't have the luxury of accessing a GUI desktop at all. Vi and Emacs are the only tools that come with every Linux distro that work in text mode, so learning one or the other is mandatory. (For Windows and DOS aficionados, think MS-DOS's Edit and Edlin programs.) Which one's better? Vi and Emacs fans are fond of waging war on that question. In this edition of Linux Explorers, we're picking your text editor for you. This one is all about the Vi text editor, because that's the one we prefer. Vi was originally developed by William Joy at Berkeley University and first officially included in AT&T System 5 Unix. It started out as a line-oriented editor for dumb terminals. The Vi editor, or Vi iMproved (VIM) is an enhancement by Bram Moolenaar; it's the version that people use today.

Linux Vi editor has three modes:-

1. Command mode:-(commands which cause action to be taken on the


file Press the i character to change to Insert mode).

2. Insert mode:-(entered text is inserted into the file Press the <Esc>
(Escape) key to go to the command mode).

3. Replacement mode:-(cursor movement, copy, paste, commands


which cause action to be taken on the file Press the r character to change to replacement mode).

23

Getting started
To start Vi, open a terminal or console and simply type "vi" (without the quotation marks) followed by the name of any existing file or a new file you want to create. Vi works in two main modes, one for editing text and the other for giving commands. To switch between the two modes you use the I and Esc keys. The program opens in the Command mode, which is used for cursor movements, delete, cut, copy, paste, and saving changes. The Insert mode is what you'll work in most of the time. You use it to make changes in an open file. Enter the Insert mode by pressing the I key. Newer Vi versions will display the word "INSERT' on the bottom line while you're in Insert mode. Press the Esc key to switch Vi back to Command mode. As soon as you hit the Esc key the text "INSERT" on the bottom line disappears. You save your changes to the open file from the Command mode. Press Shift-ZZ to save.

Basic commands for the Linux vi Editor


The Linux VI Editor is a text based editor used Linux, useful for editing configuration files or creating plain text documents. Vi is started by simply entering "vi" (it is case sensitive) at the linux prompt, and once started is exited using ESC:q! to abandon changes or SHIFT+ZZ (case sensitve) to save changes. If you want to edit (or create) a specific file uses the command "vi /path_to_file/name_of_file". "path_to_file" must already exist as vi will not make any required directories.

24

Command
i a /word n w e b 3b yy dd 3dd D dw x o O CTRL-w u U

Description
insert mode, (ESC to exit insert mode) allows text to be entered on the screen Append to right mode Move to the occurrence of "word" Locate the next occurrence Advance to the next word Advance to the next end of a word Move to the previous word Move backward 3 words Copy line (then move cursor and use p to paste after current cursor line) delete line delete 3 lines Delete remainder of a line Delete word Delete character Open space for new line below the cursor line Open a line above the cursor Move back a word in append mode Undo last Undo all changes to current line
25

In command mode, repeat the last text changing the command on the current line

:w newfilename save the file to newfilename from the command mode :wq :q! r J s cw c cc h H L M G 0 (zero) view filename set number set nonumber save and quit quit without saving replace then typa a character ot be replaced with r then return to break up a line join 2 lines substitute (sentence) typed text over a character, ESC when done change word change part of a line from the cursor to the end of the line substitute new text for aline, ESC when done move the cursor back one space move the cursor to the highest line on the screen move the cursor to the lowest line on the screen position the cursor at the midpoint on the screen last line in the file Move the cursor to the beginning of the line it is on Open a file for viewing only Turn on line numbers Turn off line numbers

26

27

Introduction of Linux Shell:A shell is a program that acts as an interface between the user and the kernel whereas the kernel acts as the interface between the hardware and the user while working with Linux operating system. The shell interprets the commands typed at the terminal by the user and converts those commands into the kernel readable from. The shell is also termed as a Command-line interface through which the commands are interpreted. Various types of shells as the Bourne and the C are provided by the Linux operating system. All the shells installed on the Linux operating system are viewed using the files /etc/shells.

Various types of shells available in Linux are:


Bourne shell:-The executable file for the bourne shell is sh and is installed at /bin/sh.

C shell:-The executable file for the C shell is csh and is installed at


/bin/csh.

Korn shell:-The executable file of the Korn shell is ksh and is installed
at /bin/ksh. The korn shell incorporates the features of both the Bourne shell and the C shells.

bash shell:-The executable file of the bash shell is /bin/bash. Tcsh shell:-The Toms C (Tcsh) shell shell is an enhancement to the C
shell. The Tcsh shell is executed by typing either csh or tcsh at the shell prompt. ASH shell:-The ASH shell is a lightweight Bourne shell, which is usually suitable for computers that have limited memory.

28

Shell Scripiting:All shell statements and LINUX commands can be entered in the command line itself. However, when a group of commands has to be executed regularly, they are better stored in a file. All such files are called shell scripts, shell programs, or shell procedures. The shell has a whole set of internal commands that can be stringed together as a language, with its own variables, conditionals and loops. What makes shell program powerful is that an external command can also be used as a control command for any of the shells constructs. In which used all Conditional Statements like as if Statement, case Statement; Iteration Statements: for Statement, while Statement, until Statement, etc. All Statements explain with examples.

Numeric Comparison with test:When you utilize if to evaluate expressions, the test statement is often as its control command. Moreover, the relational operators also have a different form when used by test. They always begin with a (hyphen), followed by a twocharacter word, and enclosed on either side by white space. The complete set of operators is shown in Table.

Table: Relational Operators Used by if


Operator
-eq -ne -g -ge -lt -le

Meaning
Equal to Not equal to Greater than Greater than or equal to Less than Less than or equal to

29

if Statement:-

The if statement, like its counterpart in other programming languages, takes two-way decisions, depending on the fulfillment of a certain condition. Every if has must have a corresponding fi. In the shell, the statement uses the following form. If condition is true
then

execute commands else execute command fi

Example: WAP Using if statement in shell scripting.


a=4 If [ $ a gt 3 ] ; then echo The value of a is $a fi

Output:The value of a is 4

30

if-elif: Multi-way Branching:-

if also permits multi-way branching; you can evaluate more conditions if the previous condition fails. The shell also has a if-then-fi, if-then-else-fi, and if-thenelif-then-else-fi, where you can have as many elif s as you want, while the else remains optional

Example: WAP to print big number using if-then-else-fi in shell scripting.


Echo n Enter the 1st number= read x echo n Enter the 2nd number= read y echo Enter the 3rd number= read z if [ $x gt $y ] && [ $x gt $z] ; then echo x is big number elif [ $y gt $x ] && [ $y gt $z] ; then echo y is big number elif [ $z gt $x ] && [ $z gt $y] ; then echo z is big number else echo Equal number fi

31

Output:Enter the 1st number=15 Enter the 2nd number=49 Enter the 3st number=35 y is big number

Example: WAP to print the Any Day of week using ifthen-elif-then-else-fi in shell scripting.
Echo n Enter Any no= read a if [ $a eq 1 ] ; then echo RED elif [ $a eq 2 ] ; then echo GREEN elif [ $a eq 3 ] ; then echo YELLOW elif [ $a eq 4 ] ; then echo BLUE elif [ $a eq 5 ] ; then echo ORANGE elif [ $a eq 6 ] ; then echo PINK else
32

echo WROND DATA fi

Output:Enter any no=6 PINK

THE case statement:-

The case statement is the second conditional offered by the shell. The statement matches an expression for more than one alternative, and user a compact construct to permit multi-way branching. It also handles string tests, but in a more efficient manner than if. The general syntax of the case statement is as follows: case expression in pattern1) execute commands ; ; pattern2) execute commands ; ; pattern3) execute commands ; ; esac

case matches the expression first for pattern1, and if successful, executes the commands associated with it. If it doesnt then it falls through and matches pattern2, and so on. Each command list is terminated by a pair of semi-colons, and the entire construct is closed with esac (reverse of case).

33

Example: WAP to print the Arithmetic Operator using the case statement in shell scripting.
echo n Enter the 1st number= read a echo n Enter the 2nd number= read b echo n Press any no 1 to 5= read c case $c in 1) let d=$a+$b; echo Sum of two nos=$d ;; 2) let d=$a-$b; echo Subtraction of two nos=$d ;; 3) let d=$a*$b; echo Multiplication of two nos=$d ;;

4)
34

let d=$a/$b; echo Division of two nos=$d ;; 5) let d=$a%$b; echo Modulus of two nos= $d ;;

*) echo Default value ;; esac

Output:Enter the 1st number= 20 Enter the 2nd number=2 Press any no 1 to 5=3 Multiplication of two nos=40

35

The for statement:The for loop is different in structure from the ones used in other programming languages. There is no next statement here, neither can a step be specified. Unlike while and until, it doesnt test a condition, but uses a list instead. The syntax of this construct is as follows: for variable in list do execute commands done The loop body is the same, but the additional parameters here are variable and list. The loop body is executed as many times as there are items in the list.

A simple Example of for loop.


for x in 1 2 4 5 do echo The value of X is $x done

Output:The value of X is 1 The value of X is 2 The value of X is 4 The value of X is 5

36

Example: WAP to print the any Table using for loop in shell scripting.
echo n Enter any no you want display the Table= read a p=1 i=1 for i in 1 2 3 4 5 6 7 8 9 10 do l=$a*$i echo n$l= let p=a*i echo $p let i+=1 done

Output:Enter any no you want display the Table=7 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42

37

7*7=49 7*8=56 7*9=63 7*10=70

Example: To print the following output using for loop in shell scripting.
* * * * * * * * * * J=1 for i in 1 2 3 4 do for j in 1 2 3 4 do if [ $j-le $i ] ; then echo n * fi let j+=1 done echo done

38

Example: To print the following output using for loop in shell scripting.
1 1 1 1 2 2 2 3 3 4

J=1 for i in 1 2 3 4 do for j in 1 2 3 4 do if [ $j le $i ] ; then echo n $j fi let j+=1 done echo done

Example: WAP to print the Factorial using for loop in shell scripting.
echo n Enter any number= read a fact=1 for i in 5 4 3 2 1 do let fact=fact*i
39

done echo Factorial is=$fact

Output:Enter any number=5 Factorial is=120

while statement:The while statement should be quite familiar to most programmers. It repeatedly performs a set of instructions till the control command returns a true exit status. The general syntax of this command is as follows: while condition is true do execute commands done The set of instructions enclosed by do and done are to be performed as long as the condition remains true.

Example: A simple program using while loop in shell scripting.


echo n Press any number= read a while [ $a lt 5 ] do echo AMAN let i+=1 done

Output:Press any number=4


40

AMAN AMAN AMAN AMAN

Example: WAP to print the Factorial using while loop in shell scripting.
echo n Enter any number= read n i=1 fact=1 while [ $i gt $n] do let fact=fact*i let i+=1 done echo Factorial is=$fact

Output:Enter any number=5 Factorial is=120

41

También podría gustarte