You are on page 1of 22

UNIX and Shell Programming BTIT-406

Shri Vaishnav Vidhyapeeth Vishwavidyalaya, Indore


Department of Computer Science
Shri Vaishnav Institute of Information and Technology

UNIX and Shell Programming Lab File


Session = 2020-24
SUBJECT NAME =UNIX and Shell Programming
SUBJECT CODE = BTIT-406
Enrollment No. =20100BTBDCEIM07387
B. TECH (C.S) ~IVth SEM

SUBMITTED TO: - SUBMITTED BY: -


Mrs. Sukrati Agrawal Akshat Modi

Page | 1
UNIX and Shell Programming BTIT-406

INDEX
S.no Experiment Name Date Pg. No Remarks

1. Perform installation of UNIX operating system. 3-3

2. Study of UNIX operating system. 4-4

3. Compare of UNIX operating system with 5-5


Windows, Linux and Mac.

4. Study and execution of UNIX general purpose 6-8


utility command.

5. Study and execution of various file and 9-12


directory handling command.

6. Study and execution of networking command. 13-14

7. Working with vi editor. Creating and editing a 15-15


text file with vi editor using the standard vi
editor command.
8. Write a shell script for a C program to print 16-16
Hello.

9. Write a shell script for a calculator to perform 17-17


basic arithmetic calculation.

10. Pattern search and sorting in file. 18-18

11. Print Hello in shell script. 19-19

12. Use a variable that takes input name from user 20-20
and print it on the console.

13. Check whether the input is even or odd. 21-21

14. A program to print input of 10 numbers and 22-22


print their sum.

Page | 2
UNIX and Shell Programming BTIT-406

Experiment= 1
Perform installation of UNIX operating system

Steps to install Unix operating system are:


1) Step 1: Before you install.
2) Step 2: Log in into the system.
3) Step 3: Insert Product CD or Download Product files.
4) Step 4: Create the installation directory.
5) Step 5: Put the license File in the installation.
6) Step 6: Start the installer.
7) Step 7: Review the installer.
8) Step 8: Verify the installation Directory name.

Page | 3
UNIX and Shell Programming BTIT-406

Experiment= 2
Study of Unix operating system

Unix is a computer operating system that was developed to be portable, multi-tasking in a time-
sharing configuration. Today many different varieties of Unix exist. However, all Unix system have
same common characteristics, like use of plain text for storing data, have a hierarchical file
system, and etc. The Unix system also comprises a master control program, the Kernel.
Functions of Unix operating system:
1) Effective optimization of resources- It optimizes the task of effective utilization of
resources.
2) Memory Management- This includes swapping of pages in memory.
3) Process Management- It includes scheduling of processes.
4) Maintaining file system- It maintains files by removing junk files,
5) Handling interrupt- It handles the signals caused by hardware.
Advantages of Unix:
1) Portable from large to small systems because written in C.
2) Powerful utilities are brief, single-operation commands that can be in combined in single
command line.
3) Device independent—can be configured to run any device because it includes device
drivers in OS.
Disadvantages of Unix:
1) Unix is difficult software for novice microcomputer users to understand.
2) Limited amount of application programs is available for Unix.
3) It is not standardized, therefore an application program written for one Unix system might
not work with another.

Page | 4
UNIX and Shell Programming BTIT-406

Experiment= 3
Compare Unix operating system with Windows, Linux and Mac.
Windows Linux Mac Unix
Manufacturer Microsoft Inc. It is developed as Apple In. for Three biggest
opensource OS their Macintosh distributions are
under the GNU line of computer Solaris running
project by the systems. (Oracle), AIXon
Originator, Linux (IBM) and HP.
Torvalds and many
more.
Development Developed and Linux isMac OS was Unix systems has
and distributed by opensource and designed only to various flavours,
Distribution Microsoft. distributed by be deployed by mostly by AT & T.
various vendors. Apple
Computers.
Computer C86, x86-64. X86, x86-64, 68k, PowerPC. Available on PA-
Architecture PowerPC, SPARC, RISC and Itanium
Supported APLHA, others. machines, Solaris
also available for
x86/x64 based
systems.
Target Workstation, Desktop/Server Workstation, 8086 Unix
System Type Personal depends on Personal systems, PDP-
Computer, Media distribution. Computer, 11/70 systems.
Centre, Tablet PC, Embedded.
Embedded.
File System NTFS, FAT & exFAT ext2, ext3, ext4, HFS+, HFS, MFS Jfs, gpfs, hfs,
Supported with ISO 9660, ReserFS, FAT, ISO AFP, with ISO hfs+, ufs, xfs, zfs
UDF. 3rd Party 9660, UDF, NFS 9660, FAT, UDF. format.
driver that and others.
supports file
system ext2 and
ext3, ReserFS and
HFS.
User Friendly Very user friendly. Depends on Very user Unix is user-
for Lay Layers distribution. More friendly. friendly. It’s just
friendly to users. choosy about.

Page | 5
UNIX and Shell Programming BTIT-406

Experiment= 4
Study and execute of Unix general purpose utility commands.

Some general-purpose utility commands are:


1. Echo= With help of this command, user can display the text given as an argument.
Syntax= echo [string]

2. Man= It is used to display the user manual of any command that we can run on the terminal.
Syntax= man command_name

3. Whoami= It is used to display the username of the current user when this command is
invoked.
Syntax= whoami

Page | 6
UNIX and Shell Programming BTIT-406

4. Cal= This command is used to display the calendar.


Syntax= cal
Option: -y= show calendar of complete year
-3= show calendar of previous, current and next month
-j= show calendar of current month in Julian calendar

5. Date= This is used to display system- specific date, time, month and year.
Syntax= date
Options: -u= display time in GMT/UTC Time Zone
-d= display date string in the format of date
-s= used to set the time and date
-f file_name= display last time you edited the file

6. History= It is used to view previously executed command.


Syntax= history

Page | 7
UNIX and Shell Programming BTIT-406

7. Tty= It is used to display information about the terminal type.


Syntax= tty
Options: -s= Print nothing only returns an exit status
--help= it will display the help message and exit
-version= Prints the version information and exits

Page | 8
UNIX and Shell Programming BTIT-406

Experiment= 5
Study and execution of various files and directory handling commands.

List of some file and directory handling commands are:


1. Mkdir= With help of this command, user can create a new directory.
Syntax= mkdir directory_name

2. Rmdir= With help of this commands, user can remove an empty directory.
Syntax= rmdir directory_name

3. Pwd= This is used to identify what is your current working directory.


Syntax= pwd

4. Cd= With help of this command, the user can move around the direcectories.
Syntax= cd\

5. Ls= This is used to list the names of files and sub-directories in the current directory.
Syntax= ls
Options: -l= this is used for long list
• -a= this option list the hidden files
• -d= this option is use to list only directories
• -r= this is used to get output in reverse order
Page | 9
UNIX and Shell Programming BTIT-406

• -R= this is used to recursively listing of all files in sub=directories

6. mv= This is used to change the name of a file or directory. With the help of this command, we
can also move a file from one directory to another.
Syntax= mv source destination

7. find= This is very powerful. It can search the entire file system for one or more files that you
specify to look for.
Syntax= find /- file_name -type f -print

8. cp= This command is use to copy a file.


Syntax= cp source destination

9. rm= This is used to delete a file.


Syntax= rm file_name

Page | 10
UNIX and Shell Programming BTIT-406

10.Pg= With the help of this command, the contends of the file on the screen, one page at a time.
Syntax= pg filename

11.Cat= This command is used to display the contents of a file and create a file.
Syntax= cat file_name

12.File= This command is used to know the type of a particular file.


Syntax= file file_name

13.More= This command is used to view one output screen at a time. If the contend of file is
more than one output screen, issuing cat and man command will display the last screen.
Syntax= more file_name

14.Gzip= To conserve disk space we need to zip some files. This command is used to compress
the file.
Syntax= gzip file_name
To decompress syntax= gunzip file_name.gz
Page | 11
UNIX and Shell Programming BTIT-406

15.Tar= It is used for creating a disk archive that contains a group of files or an entire directory
structure we need to use tar.
Syntax= tar -cvf name_of_archive_to_be_created.tar file1 file2 …….
To extract from archive= tar -xvf name_of_archive
Options: -c= create an archive
-x= abstract file from archive
-t= display file in archive
-f= specify the archive file

16.Zip= It compress and archive file together. Zip requires the first argument to be the
compressed file name. The remaining arguments are interpreted as file and directories to be
created.
Syntax= zip file_name_to_be_zipped.zip file1 file2 ….
Syntax to unzip= unzip zip_file_name

17.touch= This command is used to create an empty file.


Syntax= touch file_name

Page | 12
UNIX and Shell Programming BTIT-406

Experiment= 6
Study and execution of networking commands

1. ipconfig= view network configuration, it displays the current network adaptor


configuration. It is handy to determine if you are getting transmit or receive errors.
Syntax= ipconfig

2. ping= It sends packets of information to a user-defined source. If the packets are received,
the destination device sends packets back.
Syntax= ping

3. netstat= most useful and very versatile for finding a connection to and from the host.
Syntax= netstat

Page | 13
UNIX and Shell Programming BTIT-406

4. finger= view user information, display a user login name. real name, terminal name and
write status, this is pretty old Unix command and rarely used nowadays.
Syntax= finger

Page | 14
UNIX and Shell Programming BTIT-406

Experiment= 7
Working with vi editor, creating and editing a text file with vi editor using standard vi editor
commands.

The VI editor is the most popular and classic text editor in the Linux family. Below, are some
reasons which make it a widely used editor –

1) It is available in almost all Linux Distributions

2) It works the same across different platforms and Distributions

3) It is user-friendly. Hence, millions of Linux users love it and use it for their editing needs
Nowadays, there are advanced versions of the vi-editor available, and the most popular one
is VIM which is Vi Improved. Some of the other ones are Elvis, Nvi, Nano, and Vile. It is wise to
learn vi because it is feature-rich and offers endless possibilities to edit a file. To work on VI editor,
you need to understand its operation modes. They can be divided into two main parts.

vi Command mode:
• The vi editor opens in this mode, and it only understands commands
• In this mode, you can, move the cursor and cut, copy, paste the text
• This mode also saves the changes you have made to the file
• Commands are case sensitive. You should use the right letter case.
vi Editor Insert mode:
• This mode is for inserting text in the file.
• You can switch to the Insert mode from the command mode by pressing ‘i’ on the keyboard
• Once you are in Insert mode, any key would be taken as an input for the file on which you are
currently working.
• To return to the command mode and save the changes you have made you need to press the
Esc key

Page | 15
UNIX and Shell Programming BTIT-406

Experiment= 8
Write a shell script for a C program to print Hello

The Bash script is a text file that contains a set of instructiond that can be executed in the
terminal. In any programming, the first program that we learn to write is “Hello World”.

Page | 16
UNIX and Shell Programming BTIT-406

Experiment=9
Write a shell script for a calculator to perform basic arithmetic calculation

Create a file as vi calc.sh

Page | 17
UNIX and Shell Programming BTIT-406

Experiment=10
Pattern search and sorting a file
The grep filter searches a file for a particular pattern of characters, and displays all lines
that contain that pattern. The pattern that is searched in the file is referred to as the
regular expression.
Syntax= grep [options] pattern [files]

Options:
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern -e exp :
Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line, with each
such part on a separate output line.
0

Sort= This command is used to sort the file.


Syntax= sort [option] [file_name]
Options: -b= ignore blanks at initial of line
-r= sort file in reverse order
-n= use numerical value to sort
-M= sort as per the calendar month specified

Page | 18
UNIX and Shell Programming BTIT-406

Experiment=11
Print Hello in Shell Script

1. Create new file in vi editor mode Command: $vi hello.sh


2. Press I to enter insert mode

3. Press esc after writing the above code and enter :w to save the file
4. Run file with command: $bash hello.sh

Page | 19
UNIX and Shell Programming BTIT-406

Experiment= 12
Use a variable that takes input name from user and print it on the console.

Output:

Page | 20
UNIX and Shell Programming BTIT-406

Experiment= 13

Check whether the input is even or odd

Code:
echo "enter a number"
read n
if [ `expr $n % 2 ` == 0 ]
then
echo "number is even" else
echo "number is odd"

fi
exit 0

Page | 21
UNIX and Shell Programming BTIT-406

Experiment= 14
Print sum of 10 numbers
Code:

i=0
sum = 0
echo "enter numbers : " while [ $i -lt
10 ]
do
read n
sum = $(( sum + n )) i=
$(( i + 1 ))
done
echo "sum of given 10 numbers is : $sum" exit 0

Page | 22

You might also like