0% found this document useful (0 votes)
10 views44 pages

Smriti (2319017) Ulp File

This document is a practical file for the UNIX and Linux Programming Lab, submitted by a student as part of their B.Tech in Computer Science and Engineering. It includes various practical exercises covering topics such as basic Linux commands, the vi editor, shell programming, GREP commands, sed, awk, and Perl programming. Each practical aims to familiarize students with different aspects of UNIX/Linux programming and includes code examples and explanations.

Uploaded by

ranavanshika2737
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views44 pages

Smriti (2319017) Ulp File

This document is a practical file for the UNIX and Linux Programming Lab, submitted by a student as part of their B.Tech in Computer Science and Engineering. It includes various practical exercises covering topics such as basic Linux commands, the vi editor, shell programming, GREP commands, sed, awk, and Perl programming. Each practical aims to familiarize students with different aspects of UNIX/Linux programming and includes code examples and explanations.

Uploaded by

ranavanshika2737
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

2319017

PRACTICAL FILE
of

“UNIX AND LINUX PROGRAMMING LAB” SUBJECT

CODE: PC-CS-306LA
SUBMITTED IN PARTIAL FULFILLMENT OF THE
REQUIREMENTS FOR THE AWARD OF
BACHELORS OF TECHNOLOGY (B.TECH.)
IN
COMPUTER SCIENCE AND ENGINEERING
(SESSION: 2019-2023)

SUBMITTED BY:
Harshita

2319023

BRANCH: Computer Science and Engineering


SUBMITTED TO:
Ms. Rajwinder Kaur
CSE Department, ACE

Department of Computer Science and Engineering


Ambala College of Engineering and Applied Research, Devsthali, Ambala
(Haryana)
Affiliated to Kurukshetra University, Kurukshetra
June 2022

1
2319017

INDEX
S.NO PRACTICAL AIM DATE SIGNATURE

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

2
2319017

Practical 1

Aim: Familiarize with Unix/Linux logging/logout and simple commands.

Linux logging in and logging out:


On switching in a UNIX environment. The Kernel program is the first program
that is loaded in the computer’s memory. From the Hard-disk. this program
triggers a chain of initialization processes at the end of which the login prompt
appears on the screen.
On entering a login name, the login program is activated login points the
password prompt on the screen and turns off character echoing. so that the
password entered is not displayed on the terminal. Login compares the
password entered with the /etc/passwd file, if they match, a program named in
file replaces the login program, the program named is usually BOURNE SHELL or
the C SHELL and is called login shell.

3
2319017

Some basic commands in Linux/Unix:


 pwd: This command is used to show the current directory.

 ls: This command is used to show all the directories and files at current
path.

Attributes:

o ls -l: shows file or directory, size, modified date and time, file or
folder name and owner of file and its permission.

4
2319017

o ls -a: same as “ls” command but also include hidden files.

o ls -n: It is used to print group ID and owner ID instead of their


names.

o ls -li: This command prints the index number if file is in the first
column.

5
2319017

o ls -r: It is used to print the list in reverse order.

 cd: used to change directory.

6
2319017

 mkdir and rmdir: “mkdir” is used to make a new directory and “rmdir” is
used to delete a directory.

 rm: used to remove a file from a directory.

 touch: used to make an empty file.

 man: it is used if we want to know about details of any command for e.g.
“man touch”.

7
2319017

 cp: used to copy file or directory from one directory to another directory.
Its syntax is like this “cp *FILE_OR_DIRECTORY_NAME*
*DIRECTORY_WHERE_WANT_TO_COPY*”.

 mv: used to move file or directory from one directory to another


directory. Its syntax is like this “cp *FILE_OR_DIRECTORY_NAME*
*DIRECTORY_WHERE_WANT_TO_MOVE*”.

8
2319017

 locate: used to locate file in the file system. It’s almost same as searching
in windows. It gives us output of the path where the file is located in.
Note – we need to install a separate package for this command to work.

 echo and cat: ”echo” is used to enter the text information in a “.txt” file
or any other editable text file. If the file is not present in the system then
the new file will be made of the name that we have given.
“cat” is used to show view the contents of that or any other file as shown
in the screenshot.

 df: used to displays the amount of disk space available on the file system
containing each file name argument. Disk space is shown in 1 K blocks by
default

Attributes:

9
2319017

o df -m: same as “df” but shows the space in Mega Bytes (MB).

o df -T: It is used to display the file system type.

 du: The Linux “du” (Disk Usage) is a standard Unix/Linux command, used
to check the information of disk usage of files and directories on a
machine.

10
2319017

 date: it’s used to show the current date.

Attributes:

o %A: full day name (i.e. Monday, Tuesday, Wednesday)

o %B: full month name (January, February, March)

o %c: locales date and time (full date and time)

11
2319017

o %d: day of month (i.e. 01, 02, 03)

o %H: hour (00, 01, 02, 21, 22, 23)

o %m: month number (1,2,3)

o %M: minute (1,2,3,57,58,59)

o %p: AM or PM

12
2319017

o %Y: year

 cal: used to show the current month’s calendar.

 who: used to display users logged in to the system.

 whoami: used to display the name of the currrent user.

 tty: used to display the file name of the terminal connected to standard
input.

13
2319017

 bc: it launches an inbuilt calculator.

 history: used to show the history of the commands used.

Attributes:

o history -c: used to clear the history of commands used.

14
2319017

 wc: used to see the word count (how many words are there) in a “.txt” or
any other file containing any text information.

 clear: used to clear termina window text. e.g. any other previous
commands typed in the terminal.
Before executing “clear” command:

After executing “clear” command:

15
2319017

Practical 2

Aim: To Familiarize with vi editor.

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 –
 It is available in almost all Linux Distributions
 It works the same across different platforms and Distributions
 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.

 Command Mode
 Insert Mode

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.
 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.

16
2319017

 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.
Launching vi Editor:
To launch the VI Editor -Open the Terminal (CLI) and type
 vi <filename_NEW> or <filename_EXISTING>
And if you specify an existing file, then the editor would open it for you to edit.
Else, you can create a new file.

After executing this command, we enter in command mode with the file name
written at bottom:

17
2319017

Press i to get into insert mode:

Here, in insert mode we have typed “Hello World!” in the vi editor.


Now, for saving this file and exiting from vi editor we will go back to command
mode by pressing ESC button on our keyboard and typing :wq (w for save and
q for quit) and hit enter.

Now our file will be saved.

18
2319017

Here are some commands that are used in command mode in vi editor:
 A - Write at the end of line (goes into insert mode)
Initial state:

After pressing A:

 dd – Delete line
Initial state:

After pressing dd:

19
2319017

We can see here that “Hope you are good.” entry was deleted from the file.

 o - Open a new line (goes into insert mode)


Initial state:

After Pressing o:

 yy - Copies the current line.


 p - Puts the copied text after the cursor.
Initial state (pressed yy):

After pressing p:

 u - Undo last change


Initial state:

After pressing u:

20
2319017

 U - Undo all changes to the entire line


 3dd - Delete 3 lines.
Initially:

After pressing 3dd:

 D - Delete contents of line after the cursor


Initially:

After pressing D:

 dw - Delete word
Initially:

21
2319017

After pressing dw:

 4dw - Delete 4 words


Initially:

After pressing 4dw:

 cw - Change word
Initially:

After pressing cw:

 x - Delete character at the cursor


Initially:

After pressing x:

 r - Replace character
Initially:

22
2319017

After pressing r:

Moving within a file:


We need to be in the command mode to move within a file. The default keys for
navigation are mentioned below else; We can also use the arrow keys on the
keyboard.

 k - Move cursor up
 j - Move cursor down
 h - Move cursor left
 l - Move cursor right

Saving and Closing the file:


We should be in the command mode to exit the editor and save changes to the
file.
 Shift+zz - Save the file and quit
 :w - Save the file but keep it open
 :q - Quit without saving
 :wq - Save the file and quit
 :q!- Quit without saving and not getting any prompt.

23
2319017

PRACTICAL – 3

Aim: Shell programing using vi editor to get and enter student details.

Shell Programing: - Usually shells are interactive that mean, they accept
command as input from users and execute them.

Script in vi editor to enter the details of a student: -

Script in vi editor to show the details of a student: -

24
2319017

PRACTICAL 4

Aim: Write a shell program to perform Arithmetic Calculations.

Source Code In Vi Editor :-

Output Of The Following Code :-

25
2319017

PRACTICAL 5

Aim: To familiarize with ‘ GREP ’ commands.

GREP Commands :- The grep filter searches a file for a particular pattern of
characters and displays all the lines that contain the pattern that is searched in
the file is referred to as regular expression. The ‘ GREP ‘ stands for Globally
Search For Regular Expression And Print Out.

Syntax :- grep [ options ] pattern filenames


Options :-
-c : This prints only the count of line that match a
pattern.
-h : Display the matched lines but do not display the
filenames.
-i : Ignore the case sensitive for matching.
-e : Specifies expression multiple times.
-E : Treats the pattern as an extended regular
expression.

Output Of The GREP Commands :-

26
2319017

Practical 6

Aim: Write a program in shell that searches a particular pattern of string from
a file. File name and pattern is taken from the user.

Source Code:

OUTPUT:

27
2319017

Practical 7

Aim: Familiarize with sed command.

SED command in UNIX stands for stream editor and it can perform lots of
functions on file like searching, find and replace, insertion or deletion. Though
most common use of SED command in UNIX is for substitution or for find and
replace. By using SED you can edit files even without opening them, which is
much quicker way to find and replace something in file, than first opening that
file in VI Editor and then changing it.
- SED is a powerful text stream editor. Can do insertion, deletion, search and
replace(substitution).

- SED command in unix supports regular expression which allows it perform


complex pattern matching.

Syntax:
$sed [option] ‘address action’ filename
Options:
- ‘-n’: by default sed displays or prints the line selected for the processing.
To supress the default printing we use -n option

- ‘-e’: To perform different actions on file in single command we use -e op-


tion

- -f: this option is required when instructions are needed to be executed


from a separate file.

Examples:
1.Here we will print the line 1 from the file name egrep-test.txt

28
2319017

2.Here we will print all lines from the given file.

3. Following will print all the lines except 3 line.

4. Following command will use extend option.

5. Following command will use context addressing.

29
2319017

This command prints the lines which contains half word.

6. Following command will delete lines from x to y.

Practical 8

Aim: Familiarize with awk programming.

Awk is a filter programming language developed in 1977 as a pattern scanning


language. It was developed by Aho, Weinberger and kornigram. One of the
important is that it operates at the field level and can access individual fields in
a line. It also accepts extended regular expression for pattern matching.

Syntax:
$awk options ‘pattern {action}’ filename

Commands:

1. Print all the lines (we can do it by using {print} or {print $0} in action)
Using print only

Using $0

30
2319017

2. Print the specified column

3. print lines according to the matched pattern

First we print records whose grades are in A

Print records whose name start with s

Print records whose name end with a

31
2319017

Print records whose name is vishakha and grade is A+ and print whose
name is smriti or marks are 80.

Now we are going to run commands using system variables


NR – number of records
NF – number of fields
ORS – output Record separator

4. usage of NR
using NR command print number of records with row number

Using NR command print total number of records

Print odd lines using NR


usage of NF
Print last line and first line using NF

32
2319017

Usage of ORS

print the average score of students

33
2319017

Practical 9

Aim: Familiarize with Perl programming.

Theory : It stands for Practical Extraction Report Language. It is a powerful and


high-level programing language. It is a kind of interpreted language. It is popular
due to its process file and text manipulation facilities and many more. It is a
scripting language and it also runs directly on command prompt. It is mostly
used for quick prototyping, software tools, graphical programing, networking
and web programing etc.

Types of variables in Perl programming :


 Scalar
 Arrays
 Hashes (Associative array variables)
Program to show the working of different variables :
 Scalar Variables - It is denoted by ‘$’ before the name of variable.
Source Code –

Output –

34
2319017

 Arrays - It is denoted by ‘@’ before the name of variable.


Source Code –

Output –

 Hashes - It is denoted by ‘%’ before the name of variable. It is also


known as ‘Associative Arrays’. It is a group of ‘key – value’ pair.
Source Code –

35
2319017

Output –

36
2319017

Practical 10

Aim: Program to remove or add values in array.

Source Code –

Output –

37
2319017

Practical 11
Aim: Program for showing arithmetic operations using Perl commands.

Source Code –

Output –

38
2319017

Practical 12
Aim: Program to find factorial of given number.

Source Code –

Output –

39
2319017

Practical 13

Aim: Familiarize with processes in UNIX/LINUX.

Theory: A program/command when executed, a special instance is provided by


the system to the process. This instance consists of all the services/resources
that may be utilized by the process under execution.
 Whenever a command is issued in Unix/Linux, it creates/starts a new
process. For example, pwd when issued which is used to list the current
directory location the user is in, a process starts.
 Through a 5 digit ID number Unix/Linux keeps an account of the pro-
cesses, this number is call process ID or PID. Each process in the system
has a unique PID.
 Used up pid’s can be used in again for a newer process since all the pos-
sible combinations are used.
 At any point of time, no two processes with the same pid exist in the sys-
tem because it is the pid that Unix uses to track each process.

Program to show the usage of process:


 Tracking ongoing processes - ps (Process status) can be used to see/list
all the running processes.

For more information -f (full) can be used along with ps

40
2319017

For single-process information, ps along with process id is used

For a running program (named process) Pid of finds the process id’s
(pids)
Fields described by ps are described as -
1. UID: User ID that this process belongs to (the person running it)
2. PID: Process ID
3. PPID: Parent process ID (the ID of the process that started it)
4. C: CPU utilization of process
5. STIME: Process start time
6. TTY: Terminal type associated with the process
7. TIME: CPU time is taken by the process
8. CMD: The command that started this process

There are other options which can be used along with ps command-
-a: Shows information about all users
-x: Shows information about processes without terminals
-u: Shows additional information like -f option
-e: Displays extended information
 Stopping a process -
When running in foreground, hitting Ctrl + c (interrupt character) will
exit the command. For processes running in background kill command
can be used if it’s pid is known.

Latest background process can be killed using kill $! Command

Terminal can be terminated using kill $$ command

41
2319017

 Signals in the UNIX/LINUX -


Signals are software interrupts that are sent to a program to notify it of
an important occurrence. User requests to unlawful memory access
errors are examples of occurrences. Some signals, such as the interrupt
signal, indicate that a user has asked the program to do an action that is
not normally part of the program’s flow of control.

Signals are shown using kill -l command.

 Other commands -
bg - A job control command that resumes suspended jobs while keeping
them running in the background.
If we use $>bg command in the terminal then it brings the most recent
process to the background.

To run a process in the background we use & with the process.


Syntax - >$ bg

42
2319017

To bring back most bg process to the foreground, use fg.


Syntax - >$ fg % name_of_command

This fg % vi command brings the vi editor to the foreground.


To bring a foreground job to the background, use below syntax.
Syntax - >$ fg %? Command_name

43
2319017

Practical 14
Aim: Familiarize with at and batch command.
‘at’ - This is a command-line utility that allows you to schedule commands to be
executed at a particular time. Jobs created with at are executed only once.
How to use at command -
Simplified syntax for at command
Syntax - >$ at [options] runtime
The at command takes the date and time (runtime) when you want to execute
the job as a command-line parameter, and the command to be executed from
the standard input.

Let’s create a job that will be executed at 7:52 am:

The command will display the job number and the execution time and date.

Use the -m to send an email even if there is no output:

‘batch’ - batch or its alias at -b schedules jobs and executes them in a batch
queue when the system load level permit. By default, the jobs are executed
when the system load average is below 1.5. The value of the load can be
specified when invoking the at daemon. If the system load average is higher the
specified one, the jobs will wait in the queue.

To create a job with batch, pass the commands you want to execute:

44

You might also like