srgoc linux

46
INDEX S.N o Date Topic Sign Remarks 1. Installation, Configuration and Customizations of UNIX/LINUX. 2. To study UNIX operating system and explain its function. 3. To study architecture of LINUX. 4. To study basic & user status UNIX/LINUX commands. 5. To study basic & user status UNIX/LINUX directory commands. 6. Study and use of commands for performing arithmetic operation with LINUX. 7. Study and use of commands for changing file permissions. 8. What is shell programming and shell script? Explain the types of shell. 9. Write a shell script and put your comments in your script and make it executable. 10. Write a shell script that stores any value you want in a variable and access the variable using commands. 11. Write a program in LINUX which uses all the arithmetic operators. 12. Write a program in LINUX which uses all the relational operators. 13. Write a program in LINUX to find out whether the number is greater or lesser. 14. What is VI editor? Execute shell commands using VI editor. 15. What is LINUX file system? 1

Upload: gaurav-singh

Post on 08-Apr-2017

136 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Srgoc linux

INDEXS.No

Date Topic Sign Remarks

1. Installation, Configuration and Customizations of UNIX/LINUX.

2. To study UNIX operating system and explain its function.

3. To study architecture of LINUX.4. To study basic & user status UNIX/LINUX

commands.

5. To study basic & user status UNIX/LINUX directory commands.

6. Study and use of commands for performing arithmetic operation with LINUX.

7. Study and use of commands for changing file permissions.

8. What is shell programming and shell script? Explain the types of shell.

9. Write a shell script and put your comments in your script and make it executable.

10. Write a shell script that stores any value you want in a variable and access the variable using commands.

11. Write a program in LINUX which uses all the arithmetic operators.

12. Write a program in LINUX which uses all the relational operators.

13. Write a program in LINUX to find out whether the number is greater or lesser.

14. What is VI editor? Execute shell commands using VI editor.

15. What is LINUX file system?

1

Page 2: Srgoc linux

Experiment No. 1

Installation, Configuration and Customizations of UNIX/LINUX.

Step 1: The first thing you should do is head to http://www.ubuntu.com/download/ubuntu/download and download Ubuntu 11.10 LiveCD. Just click the big orange Start download box.

Step 2: Using your disc burning software, burn the .iso you downloaded to a CD.

Step 3: Before you go any further, ensure all important data is backed up in case of data loss on your drives. This guide assumes you have media backups of your Windows partitioned hard drive and you are safe to proceed.

***Warning: Installing another operating system without first ensuring you have backups of your current files and operating system is a big risk. If you have no data to lose or you’ve backed up important data, you’re ready to proceed. YOU are responsible if you lose data. For those of you using Windows, and installing Linux for the first time I recommend you either use a separate hard disk that does not contain the Windows OS, or create a partition big enough for Linux within Windows using Disk Management in the Administrative Tools menu of the control panel. 30GB of hard disk space is absolutely plenty of space for you to begin exploring Ubuntu whilst at the same time having room to grow.

Step 4: Ensure you have a network cable connected, restart your computer, and boot from the CD drive.

2

Page 3: Srgoc linux

Step 5: The LiveCD will load up, and you'll be presented by the following box.

For the purposes of this guide, we will assume you've already tried Ubuntu and want to proceed with an installation, so click "Install Ubuntu."

Step 6: You'll be greeted by the "Preparing to install Ubuntu" screen, exactly as below.

3

Page 4: Srgoc linux

I recommend you select "Install third-party software" as I have done in the screenshot above. I prefer to do system updates once up and running, but if you have the extra time you can also select "download updates while installing" as well. Then click continue.

Step 7: The next screen you will see is "installation type," what you see will be dependent on whether you have an existing Windows installation or not.

I'm going to split this into three different sub-steps, to make it as simple as possible.

Step 7-A: For those installing in a virtual machine or to hard disks without an OS you will see the following screen:

4

Page 5: Srgoc linux

You have two choices:1. Erase the entire disk and use all of it for installation -- Ubuntu will automatically partition your disk and proceed with installation.2. Select "something else" and manually create your partitions (which is covered in detail in step 7-C).

If you are choosing the first option, select the radio button and then click continue, proceeding to step 8.

Step 7-B: Those of you that have current Windows installations or are going to dual-boot with another existing OS will be presented with a screen similar to below.

5

Page 6: Srgoc linux

You have three options available:1. You can choose the first option and install Ubuntu alongside your existing OS. 2. You can opt to replace your Windows installation with Ubuntu, allowing the installer to format your current partitions and automatically create new ones for Linux.3. You can choose "something else" and create your own partition scheme and sizing (covered in detail in step 7-C).

Once you have selected which route you wish to proceed with click continue and proceed to step 8.

Step 7-C: Having selected the "something else" option you will be presented with the following window,

Linux recognizes and assigns IDs to drive in a different manner to Windows. In the above image, you can clearly see my hard disk in the list. It is identified by "/dev/sda." Linux recognizes drives in the following way: • Sda = 1st drive

6

Page 7: Srgoc linux

• Sdb = 2nd drive• Sdc = 3rd drive and so on

Partitions are also shown after the drive letters. So if I had 2 partitions on my first disk, they would be identified as:• Sda1 – 1st drive, 1st partition• Sda2 – 1st drive, 2nd partition

You will not see the common Windows C: label in the disk menu in the above list. You do, however, have key things to help you recognize your Windows C: drive. Both of these can be used to identify which is your Windows disk.• The size of the disk is shown• The name of the drive is shown

This is your current partition layout for your hard disks. If you have more than one disk, they will show up as /dev/sda, /dev/sdb etc.

Firstly, identify your Windows installation. In my case, it's sda1 (which is my first hard disk, first partition). What you see depends on how you created the extra space. I just resized the Windows partition from within Windows, and left the free space ready to install Linux. I recommend using Windows or a free utility from within Windows to resize your partition as most beginners will understand it more.

Therefore, we now need create a minimum of two partitions:

Click "Add" and the following box will appear:

You will notice I have already filled out the example above to create a 10GB root partition.

You can have a maximum of 4 primary partitions, or 3 primary partitions and 1 logical (which allows

7

Page 8: Srgoc linux

for another 64 partitions). The size above is 10.00GB. e.g. 1,000 = 1GB 10,000 = 10GB (Remember to leave enough free remaining space to create your SWAP partition!)Location for new partition: e.g. do you want it at the start or end of the free space. Select beginning.Use as: Ext4 is the recommended file system for Ubuntu, much the same as NTFS is Windows. SWAP is for SWAP space.Mount point: This is where you want the partition to mount. E.g. we need a root partition, which in Linux is denoted by a "/".

Click OK once you have finished setting the partition information and you will return to your partition screen, now showing the root partition you just created. Using the same methods as before, create a SWAP partition.

I recommend you set the size of your SWAP partition to at least the size of your available RAM. If you have plenty of hard disk capacity I would suggest you use double the size. So if you have 2GB of RAM, set it to either 2GB or 4GB. For best performance it is recommended you have your SWAP partition at the beginning or end of your drive.Once you have done that, you should be looking at something like below.

So to re-cap the above, (in my example) we have the following:

• /dev/sda1 is your Windows partition.• /dev/sda2 is your new root partition (Windows equiv. of C.• /dev/sda3 is your SWAP space.

Once you are happy with the changes you have made, click install now and proceed to the next step.

8

Page 9: Srgoc linux

Step 8: As the installation starts to copy the required files to the hard disk, you will be presented with a screen to select your locale. It should automatically find where you are, as it has for me already.

Just double check it is correct, and then select continue.

Step 9: The next screen to appear will be keyboard layout.

Ensure the correct option is selected, above you will see the correct (and default UK) selection has been automatically made for me.

9

Page 10: Srgoc linux

Step 10: You will now be greeted by the "who are you" screen, ready for you to fill out with your user details.

The computers name and username will automatically populate when you type your full name. You can however edit them as you please. Fill in the details and then click continue.

You can opt to have Ubuntu automatically log in for you -- even with a password set -- or you can choose the traditional option requiring a password to log in. You really don't need to choose the encrypt option unless you're installing on a laptop and are dealing with highly secure information.

Step 11: The installation information screens will now appear as Ubuntu continues the installation:

10

Page 11: Srgoc linux

Step 12: Once installation has finished, you will be presented with the following box

Select "restart now" and when requested, remove your installation CD, then press enter to reboot.

Step 13: For those of you that have Ubuntu as the only OS the computer will boot directly into Linux. If you're dual-booting, you will see the GRUB menu appear similar to below.

11

Page 12: Srgoc linux

Hit enter, to select the first option and load your newly installed Ubuntu OS.

Step 14: For those that elected to automatically log into Ubuntu, you will go straight to the desktop in Step 15. For everyone else, you will be greeted with the new login manager for Ubuntu.

Enter your password, and hit enter to login to your desktop.

Step 15: Your desktop should look like this.

12

Page 13: Srgoc linux

Step 16: Before we proceed further, let's check for updates. Click on the power button on the top right corner of the screen and select "check for updates," or words to that effect.

I'd already run updates on this install so the example above is displaying "software up to date," but the picture highlights where you need to select anyway.

Upon selecting the update option, the update manager will appear, as below.

13

Page 14: Srgoc linux

If it comes up with no available updates, just select "check" again to verify that it is correct. Having done the same thing myself, I was presented with the updates you see above. For those that opted to install updates during installation it is unlikely there will be further updates required.

You might be asked to enter your password to confirm changes. If prompted, enter your password and click OK. The same is true of any notifications that may appear during updating the OS.

Once complete select close, and restart Ubuntu. The power button is located on the top right corner of the screen. Click this and select shutdownStep 17: No install is complete without full support for mp3s, core MS fonts, DVD playback codecs, Flash and Java, so let’s go ahead and sort this now.

Click the black Ubuntu menu button at the top left corner of the screen and in the menu that appears, type "software center" and select the Ubuntu Software Centre. Once open, click the search bar, type "restricted" and the following should appear:

14

Page 15: Srgoc linux

Select Ubuntu restricted extras, and then click on the install button.

Authentication is much like UAC (user access control) in windows Vista and 7. It is required to elevate your user privileges to that of root (Linux administrator).

Ubuntu restricted extras will now download, sort any dependencies and install. You can check its progress by viewing the progress bar above the install button. Once finished the In Progress tab will disappear -- restart Linux. It’s not strictly necessary, but I always do it after installing this package so everything can start up properly.

Now you can enjoy your new OS and begin exploring its features.

15

Page 16: Srgoc linux

Experiment No. 2To study UNIX operating system and explain its function.

OPERATING SYSTEM: An Operating System is a set of programs that Functions as an virtual machine by presenting an interface that is easier to program than the underlying hardware acts as resource management through orderly and controlled allocation of the processors, memories, and I/O devices among the programs competing for it.

Types of Operating System:

1. Single User: The system will have its own hard disk, memory, CPU and other resources all dedicated to a single user. Example: MS-DOS.

2. Multi User: The users having access to a multi-user system will have just a terminal and a keyboard. The other resources such as hard disk, printers are centrally located. The user is expected to simply hook onto his account, perform the work, disconnect and leave quietly. Example: UNIX.

UNIX History:

The spade work for UNIX began at AT&T Bell Laboratories in 1969 by Ken Thompson and Dennis Ritchie. The OS was initially known as UNICS (jokingly UNiplexed Information and Computing System). In 1970 UNICS finally became UNIX. In 1973, UNIX was rewritten in 1973 in C principally authored by Ritchie.

UNIX Function:

1. Multi-user system: Multi-user capability of UNIX allows several users to use the same computer to perform their tasks. Several terminals [Keyboards and Monitors] are connected to a single powerful computer [UNIX server] and each user can work with their terminals.

2. Multi-tasking system: Multitasking is the capability of the operating system to perform various task simultaneously, i.e. a user can run multiple tasks concurrently.

3. Programming Facility: UNIX is highly programmable, the UNIX shell has all the necessary ingredients like conditional and control structures, etc.

4. Security: UNIX allows sharing of data; every user must have a single login name and password. So, accessing another user’s data is impossible without his permission.

5. Portability: UNIX is portable because it is written in a high level language. So, UNIX can be run on different computers.

6. Communication: UNIX supports communication between different terminals of the same server as well as between terminals on different servers. Apart from these features, UNIX has an extensive Tool kit, exhaustive system calls and Libraries and enhanced GUI (X Window).

16

Page 17: Srgoc linux

Experiment No. 3To study architecture of LINUX.

Linux System Architecture is consists of following layers:

ORGANIZATION OF UNIX

Hardware layer: Hardware consists of all peripheral devices (RAM/ HDD/ CPU etc).Kernel: Core component of Operating System, interacts directly with hardware, and provides low level services to upper layer components. The kernel is the heart of the system, a collection of programs written in C that directly communicate with the hardware. There is only one kernel for any system. It's that part of UNIX system that is loaded into memory when the system is booted. It manages the system resources, allocates time between users and processes, decides process priorities, and performs all other tasks. The kernel, in traditional parlance, is often called the Operating system.

Shell: An interface to kernel, hiding complexity of kernel's functions from users. Takes commands from user and executes kernel's functions. The shell, on the other hand, is the "sleeping beauty" of UNIX. It is actually the interface between the user and the kernel. The shell is the agency which takes care of the features of redirection and has a programming capability of its own.

Utilities: The Tools and Applications consist of Application Software, Compilers, Database Package, 17

Page 18: Srgoc linux

Internet tools, UNIX commands, etc.

Experiment No. 4To study basic & user status UNIX/LINUX commands.1. “touch” command: Create an empty file with 0 byte. It is also used to change the access and

modified time of currently exiting file.Syntax:$touch [option] [filename]option:“-a”: update the access time of a file.“-m”: update the modification time of a file.

2. “chomd” command (Change Mode): Changes the file mode. The command is used to change the permission of an ordinary file or directory.Syntax:$chmod <nnn> filename

Num-ber Binary

Text <nnn>

Octal Permission Representation

0 000 --- No permission (All types of access are denied

1 001 --x Execute permission (Execute access is allowed only)

2 010 -w- Write permission (Write access is allowed only)

3 011 -wx Execute and write permission: 1 (execute) + 2 (write) = 3 (Write and Ex-ecute are allowed)

4 100 r-- Read permission (Read access is allowed only)

5 101 r-x Read and execute permission: 4 (read) +1 (execute) = 5 (Read and Exe-cute access are allowed)

6 110 rw- Read and write permission: 4 (read) + 2 (write) = 6 (Read and Write ac-cess are allowed)

7 111 rwx All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 (Read, Write and Execute are allowed)

3. “rm” command (Remove File): The command is used to remove file from the system.Syntax:$rm (f) filename

18

Page 19: Srgoc linux

f: remove all the files regardless of their permission.4. “mv” command (Move File): The command is used to move file from source to destination.

Syntax:$mv (F) <source> <destination>F: Do not ask whole approval or confirmation.

5. “cp” command (Copy File): The command copy the file from source to destination.Syntax:$cp <source> <destination>

6. “wc” command (Word Count): Counting the number of characters, words and lines.Syntax:$wc [option] [file]option:“-w”:words“-l”:lines“-c”:characters

7. “cat” command (Display File): This command is used to display file.Syntax:$cat <filename>

19

Page 20: Srgoc linux

Experiment No. 5To study basic & user status UNIX/LINUX directory commands.

1. “ls” command: The “ls” command lists all files in the directory.Syntax:$ls [option] [filename]option:“-a”: list all the hidden files.“-t”: list of the file name sorted by create time.“-l”: long list of the directory purpose show the owner, group, permission and file size.

2. “pwd” command (Print Working Directory): It display the current path, pathname, enables you to work out where you are in relation to all file system.Syntax:$pwd

3. “cd” command (Change Directory): The command cd directory means change the working to directory.Syntax:$cd

4. “mkdir” command (Make Directory): This command is used to create a directory.Syntax:$mkdir <directoryname>

5. “rmdir” command (Remove Directory): This command is used to remove directory.Syntax:$rmdir <directory name>

20

Page 21: Srgoc linux

Experiment No.6Study and use of commands for performing arithmetic operation with LINUX.“bc” is an arbitrary precision calculator language. A standard math library is available by command line option. If requested, the math library is defined before processing any files.bc starts by processing code from all the files listed on the command line in the order listed. After all files have been pro-cessed, bc reads from the standard input. All code is executed as it is read. (If a file contains a com-mand to halt the processor, bc will never read from the standard input.)

21

Page 22: Srgoc linux

Experiment No. 7Study and use of commands for changing file permissions.File ownership is an important component of UNIX that provides a secure method for storing files. Every file in UNIX has the following attributes:

1. Owner permissions: The owner's permissions determine what actions the owner of the file can perform on the file.

2. Group permissions: The group's permissions determine what actions a user, who is a mem-ber of the group that a file belongs to, can perform on the file.

3. Other (world) permissions: The permissions for others indicate what action all other users can perform on the file.

The Permission Indicators:While using ls -l command it displays various information related to file permission as follows:

$ls -l /home/tarun-rwxr-xr-- 1 tarun users 1024 Nov 2 00:10 myfiledrwxr-xr--- 1 tarun users 1024 Nov 2 00:10 mydir

Here first column represents different access mode i.e. Permission associated with a file or directory.The permissions are broken into groups of threes, and each position in the group denotes a specific permission, in this order: read (r), write (w), execute (x):

The first three characters (2-4) represent the permissions for the file's owner. For example -rwxr-xr-- represents that owner has read (r), write (w) and execute (x) permission.

The second group of three characters (5-7) consists of the permissions for the group to which the file belongs. For example -rwxr-xr-- represents that group has read (r) and execute (x) per-mission but no write permission.

The last group of three characters (8-10) represents the permissions for everyone else. For ex-ample -rwxr-xr-- represents that other world has read (r) only permission.

File Access Modes:

22

Page 23: Srgoc linux

The permissions of a file are the first line of defense in the security of a UNIX system. The basic build -ing blocks of UNIX permissions are the read, write, and execute permissions, which are described be-low:

1. Read: Grants the capability to read i.e. view the contents of the file.

2. Write: Grants the capability to modify, or remove the content of the file.

3. Execute: User with execute permissions can run a file as a program.To change file or directory permissions, you use the chmod (change mode) command. There are two ways to use chmod: symbolic mode and absolute mode.

Using chmod in Symbolic Mode: The easiest way for a beginner to modify file or directory permis-sions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the per-mission set you want by using the operators in the following table.

Chmod operator Description

+ Adds the designated permission(s) to a file or direc-tory.

- Removes the designated permission(s) from a file or directory.

= Sets the designated permission(s).

Here's an example using testfile. Running ls -1 on testfile shows that the file's permissions are as fol -lows:

$ls -l abc-rwxrwxr-- 1 tarun users 1024 Nov 2 00:10 abc

Then each example chmod command from the preceding table is run on testfile, followed by ls -l so you can see the permission changes:

$chmod o+wx abc$ls -l abc-rwxrwxrwx 1 tarun users 1024 Nov 2 00:10 abc$chmod u-x abc$ls -l abc-rw-rwxrwx 1 tarun users 1024 Nov 2 00:10 abc$chmod g=r-x abc$ls -l testfile-rw-r-xrwx 1 tarun users 1024 Nov 2 00:10 abc

Here's how you could combine these commands on a single line:

$chmod o+wx,u-x,g=r-x abc

23

Page 24: Srgoc linux

$ls -l abc-rw-r-xrwx 1 tarun users 1024 Nov 2 00:10 abc

Using chmod with Absolute Permissions:The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.Each permission is assigned a value, as the following table shows, and the total of each set of permis-sions provides a number for that set.

Num-ber

Binary Text Octal Permission Representation

0 000 --- No permission (All types of access are denied

1 001 --x Execute permission (Execute access is allowed only)

2 010 -w- Write permission (Write access is allowed only)

3 011 -wx Execute and write permission: 1 (execute) + 2 (write) = 3 (Write and Execute are allowed)

4 100 r-- Read permission (Read access is allowed only)

5 101 r-x Read and execute permission: 4 (read) +1 (execute) = 5 (Read and Execute access are allowed)

6 110 rw- Read and write permission: 4 (read) + 2 (write) = 6 (Read and Write access are allowed)

7 111 rwx All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 (Read, Write and Exe-cute are allowed)

24

Page 25: Srgoc linux

Experiment No. 8What is shell programming and shell script? Explain the types of shell.Shell: The shell or the command interpreter is the mediator which interprets the command that we give and then convey them to the kernel which ultimately executes them.Syntax:$echo $SHELLTypes of shell:

1. Bourne shell (sh): originally UNIX shell written by Steve Bourne of Bell Labs, which is available on all UNIX system.It is bundled with every UNIX system.

2. C shell (csh): it was created by Bill Joy. It has advantage over the Bourne shell. Provides the c like languages with which to write shell script.C shell has 1 command history feature; this is the second benefit of that comes with C shell.

3. Korn shell (ksh): shell written by David Korn of Bell Labs, which is now provided as the Standard Shell in UNIX system.It provides all the features of the C together with a shell programming language similar to that of the original Bourne Shell.It is the most efficient shell.

4. Bourne Again Shell (Bash): this is the public domain shell written by the free software foundation widely used within the academic community.Provides all the interactive features of the C shell and the Korn Shell (ksh).

25

Page 26: Srgoc linux

If Bourne Shell (sh) is used for shell programming, bash is recommended as a complete shell environment.

Experiment No. 9Write a shell script and put your comments in your script and make it executable.To write shell script and make it executable, the following procedure should be followed:

1. Firstly we have to enter in editor, .i.e., VI Editor using command “vi”.Syntax to enter in VI Editor:$vi filename

2. To executable we have to change the permission of file by using following syntax:$chmod +x filename

3. To run the executable script we have to use the following syntax:$./filename

26

Page 27: Srgoc linux

Experiment No. 10Write a shell script that stores any value you want in a variable and access the variable using commands.For writing a shell script in Linux command is used named as “script”.$scriptA variable assignment is of form variable=value (no spaces around ‘=’), but its evaluation requires the $ as prefix to the variable name.$a=50 // Here 50 is assigned to variable a$echo $a // By using command echo the value of a will show on display50$total=$a

27

Page 28: Srgoc linux

$echo $total50

Output:

Experiment No. 11Write a program in LINUX which uses all the arithmetic operators.#include<stdio.h>int main(){int a,b,c=0,op=0;printf("\nEnter the value of first number:");scanf("%d",&a);printf("\nEnter the value of second number:");scanf("%d",&b);

28

Page 29: Srgoc linux

printf("\nSelect the following to perform operation\n");printf("1. ADDITION\n");printf("2. SUBTRACTION\n");printf("3. MULTIPLICATION\n");printf("4. DIVISION\n");printf("5. EXIT\n");printf("\n\nSelect option from the above:");scanf("%d",&op);switch(op){case 1:c=a+b;printf("Sum:%d\n\n",c);break;case 2:if(a>b){c=a-b;printf("Subtraction:%d\n\n",c);}else{c=b-a;printf("Subtraction:%d\n\n",c);}break;case 3:c=a*b;printf("Multiplication:%d\n\n",c);break;case 4:if(a>=b){c=a/b;printf("Division:%d\n\n",c);}else{printf("Division is in decimal value\n\n");}break;case 5:

29

Page 30: Srgoc linux

printf("\n\n\t\tTHANK YOU\n\n");break;

default:printf("Invalid option\n\n");break;}return 0;}

Output for Addition:

Output for Subtraction:

30

Page 31: Srgoc linux

Output of Multiplication:

31

Page 32: Srgoc linux

Output of Modulo Division:

32

Page 33: Srgoc linux

Experiment No. 12Write a program in LINUX which uses all the relational operators.#include<stdio.h>int main(){int a,b;printf("\nEnter the fisrt number:");scanf("%d",&a);printf("\nEnter the Second number:");scanf("%d",&b);if(a==b){printf("\n\n%d is equal to %d\n",a,b);}if(a!=b){printf("\n\n%d is not equal to %d\n",a,b);}if(a<b){printf("\n\n%d is less than %d\n",a,b);}if(a>b){printf("\n\n%d is greater than %d\n",a,b);}/* Lets change the values of numbers */

printf("\nEnter the fisrt number:");scanf("%d",&a);printf("\nEnter the Second number:");scanf("%d",&b);if(a<=b){printf("\n\n%d is either less than or equal to %d\n\n",a,b);}else{printf("\n\n%d is either greater than or equal to %d\n\n",a,b);}

33

Page 34: Srgoc linux

return 0;}

Output:

34

Page 35: Srgoc linux

Experiment No. 13Write a program in LINUX to find out whether the number is greater or lesser.#include<stdio.h>int main(){int a,b,c;printf("\nEnter the first number:");scanf("%d",&a);printf("\nEnter the second number:");scanf("%d",&b);printf("\nEnter the third number:");scanf("%d",&c);/* To find largest number */if( a>b && a>c ){printf("\n\n%d is the largest number\n\n",a);}elseif( b>c && b>a ){printf("\n\n%d is the largest number\n\n",b);}else{printf("\n\n%d is the largest number\n\n",c);}/* To find smallest number */if( a<b && a<c ){printf("\n\n%d is the smallest number\n\n",a);}elseif( b<c && b<a ){printf("\n\n%d is the smallest number\n\n",b);}

35

Page 36: Srgoc linux

else{printf("\n\n%d is the smallest number\n\n",c);}return 0;}

Output:

36

Page 37: Srgoc linux

Experiment No. 14What is VI editor? Execute shell commands using VI editor.There are many ways to edit files in UNIX and for me one of the best ways is using screen-oriented text editor vi. This editor enables you to edit lines in context with other lines in the file. Now days you would find an improved version of vi editor which is called VIM. Here VIM stands for ViIMproved.

The VI is generally considered the de facto standard in UNIX editors because:

1. It's usually available on all the flavors of UNIX system.2. Its implementations are very similar across the board.3. It requires very few resources.4. It is more user friendly than any other editors like ed or ex.

You can use vi editor to edit an existing file or to create a new file from scratch. You can also use this editor to just read a text file.Starting the VI Editor:

There are following way you can start using VI editor:

Command Description

vi filename Creates a new file if it already does not exist, otherwise opens existing file.

vi -R filename Opens an existing file in read only mode.

view filename Opens an existing file in read only mode.

Following is the example to create a new file testfile if it already does not exist in the current working directory:

$vi abc

As a result you would see a screen something like as follows:

|~

37

Page 38: Srgoc linux

~~~~~~~~~~~"abc" [New File]

You will notice a tilde (~) on each line following the cursor. A tilde represents an unused line. If a line does not begin with a tilde and appears to be blank, there is a space, tab, newline, or some other non-viewable character present.

So now you have opened one file to start with. Before proceeding further let us understanding few minor but important concepts explained below.

Operation Modes:While working with VI editor you would come across following two modes:1. Command mode: This mode enables you to perform administrative tasks such as saving files,

executing commands, moving the cursor, cutting (yanking) and pasting lines or words, and find-ing and replacing. In this mode, whatever you type is interpreted as a command.

2. Insert mode: This mode enables you to insert text into the file. Everything that's typed in this mode is interpreted as input and finally it is put in the file.

The vi always starts in command mode. To enter text, you must be in insert mode. To come in insert mode you simply type i. To get out of insert mode, press the Esc key, which will put you back into command mode.Hint: If you are not sure which mode you are in, press the Esc key twice, and then you'll be in com -mand mode. You open a file using VI editor and start type some characters and then come in com-mand mode to understand the difference.

Getting Out of VI:The command to quit out of vi is: q. Once in command mode, type colon, and 'q', followed by return. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ig -nore this message, the command to quit out of vi without saving is: q! This lets you exit vi without saving any of the changes.The command to save the contents of the editor is: w. you can combine the above command with the quit command, or: wq and return.

38

Page 39: Srgoc linux

The easiest way to save your changes and exit out of vi is the ZZ command. When you are in com-mand mode, type ZZ and it will do the equivalent of: wq.

Experiment No. 15What is LINUX file system?A file in UNIX is nothing but a storehouse of information and everything is treated as a file by UNIX. The files can be broadly classified as follows:

1. Ordinary files: Contains stream of data. All data, text, source programs, object and executable code, commands fall into this category.

2. Directory files: Contains no external data. It contains an entry, name of the file and its inode (identification number) for each file and subdirectory under that directory. Directory files are not created by the user but by the UNIX system itself.

3. Device files: Even physical devices are treated as files. These are special in the sense that any output directed to it will be reflected onto the respective device.

UNIX File System

39

Page 40: Srgoc linux

All files in UNIX are related to one another. The file system of UNIX resembles a tree that grows from top to bottom as shown in the figure. The file system begins with a directory called root (at the top). The root directory is denoted by a slash (\). Branching from root there are several directories such as bin, lib, etc, tmp, dev. Each of these directories contains several sub-directories and files.

40