0% found this document useful (0 votes)
12 views

Linux Lab

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Linux Lab

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

PRACTICAL : 1

Introduction:
LINUX is an open-source operating system used extensively worldwide. It is free and its source code
is available for everyone under GNU (General Public License).
Linux as an Operating System provides a wide range of features that can be used by a wide range of
systems such as servers and computers.
Android OS is also based on the Linux kernel that extends its usage for building embedded systems
and automation systems.

Features of Linux Operating Systems :


The following are the features of the Linux Operating system:
1. Free and Open-Source
Linux is completely free of cost, and expenses are never a hindrance to using it as an operating system.

Linux is open-source. This means that modification of code, analysis of codes, redistribution of codes,
or selling copies of the enhanced codes can be done by anyone in the world provided they come under
the same license where the license also costs no charge.

Linux operating system is released under the GNU (General Public License) and is now one of the
largest open-source projects worldwide.

2. Extremely Flexible
Linux has incorporated itself into embedded products like watches, digital equipment and
supercomputing servers.
There are no prerequisites for installing an entire Linux suit. It allows a user to install only the
components that are required by the user.

3. Lightweight Infrastructure
Linux consumes lesser storage space, and its installation requires around 4GB to 8GB of disk space.
Memory footprint or the amount of memory (RAM) used by the software while running is also less
and it is compatible with all kinds of file formats like text files, audio files, video files, graphic formats,
etc.

4. Graphical User Interface (GUI)


Even though Linux works on using the command line interface but it can be converted to be used like
windows having a Graphical user interface. This is done mostly by installing packages. The most
common way of having a GUI on the Linux environment is to log in to Ubuntu server and install its
desktop environment.

5. End-to-end encryption
Linux allows end-to-end encryption while accessing data thus storing public keys in the server. All
data is password protected and provides authentication to users. It also allows many security features
and provides file permissions, a secure shell, etc.

Abhishek Kumar Singh 0187CS221016 1


6. Portable Environment
Linux works on any kind of environment and doesn't depend on the device being high-ended or low-
ended. A large number of users can simultaneously use it anytime, anyplace, and on multiple devices.
It supports all kinds of hardware to work on.

7. Shell/ Command-line Interface


The Linux command line interpreter is known as Shell that provides an interface between the user and
kernel which then executes programs known as commands.

Hence, Linux uses Command-line interface to carry out the execution of tasks which is comparatively
more efficient to execute and takes lesser time. It also takes lesser space in the memory,

8. Customized keyboard
Linux is available in all languages which makes it acceptable worldwide. That is why language can be
changed from the keyboard according to the requirements and preferences.

9. Frequent New Updates


Linux operating system provides a wide range of accessible software updates that can be deployed and
used according to requirements.

10. Hierarchical File System


Linux comes with a well-defined file structure where user files are arranged in a definite directory
structure. According to the type of files, the directories are categorized as Binary directories,
configuration directories, Data directories, memory directories, Usr(Unix System Resources),
var(variable directory) and non standard directories.

11. Multi-user and Multi-programming


Linux allows multiple users to access the system resources at the same time and allows multiple
applications to run at the same time.

Abhishek Kumar Singh 0187CS221016 2


Applications of Linux
Several quantitative studies of open-source/free software concentrate on topics, such as reliability and
market share, with many studies examining Linux specifically. The Linux market is developing, and
the Linux OS market size is supposed to see a development of 19.2% by 2027, reaching 15.64 billion
dollars, compared to 3.89 billion in 2019. Proponents and analysts attribute the associative Linux
success to its freedom, low cost, reliability, and security from vendor lock-in.

Linux Distributions List


There are on an average six hundred Linux distributors providing different features. Here, we'll discuss
about some of the popular Linux distros today.

1) Ubuntu
It came into existence in 2004 by Canonical and quickly became popular. Canonical wants Ubuntu to
be used as easy graphical Linux desktop without the use of command line. It is the most well known
Linux distribution. Ubuntu is a next version of Debian and easy to use for newbies. It comes with a
lots of pre-installed apps and easy to use repositories libraries.

Earlier, Ubuntu uses GNOME2 desktop environment but now it has developed its own unity desktop
environment. It releases every six months and currently working to expand to run on tablets and
smartphones.

2) Linux Mint
Mint is based on Ubuntu and uses its repository software so some packages are common in both.
Earlier it was an alternative of Ubuntu because media codecs and proprietary software are included in
mint but was absent in Ubuntu. But now it has its own popularity and it uses cinnamon and mate
desktop instead of Ubuntu's unity desktop environment.
Abhishek Kumar Singh 0187CS221016 3
3) Debian
Debian has its existence since 1993 and releases its versions much slowly then Ubuntu and mint.
This makes it one of the most stable Linux distributor.
Ubuntu is based on Debian and was founded to improve the core bits of Debian more quickly and
make it more user friendly. Every release name of Debian is based on the name of the movie Toy
Story.

4) Red Hat Enterprise / CentOS


Red hat is a commercial Linux distributor. There products are red hat enterprise Linux (RHEL) and
Fedora which are freely available. RHEL is well tested before release and supported till seven years
after the release, whereas, fedora provides faster update and without any support.

Red hat uses trademark law to prevent their software from being redistributed. CentOS is a community
project that uses red hat enterprise Linux code but removes all its trademark and make it freely
available. In other words, it is a free version of RHEL and provide a stable platform for a long time.

5) Fedora
It is a project that mainly focuses on free software and provides latest version of software. It doesn't
make its own desktop environment but used 'upstream' software. By default it has GNOME3 desktop
environment. It is less stable but provides the latest stuff.

Abhishek Kumar Singh 0187CS221016 4


PRACTICAL : 2
Describe the following user management commands with syntax and examples
1. mkdir
2. ls
3. cd ..
4. Cd
5. tree
6. mkdir -p
7. rmdir
8. rm -rf

1. mkdir : This command is used to create a directory in current file system or directory.
Syntax :
[ ]# mkdir <directory name> 
Eg.
[ ]#mkdir sistec 

2. ls : List , this command is used to show all directories and files in current file system or
directory
Syntax :
[ ]# ls 
Sistec f1 f2 f3 dos.c cs

3. ls -l : Long List , this command is used to show all directories and files in current file
system or directory with certain information.
Syntax :
[ ]# ls -l 
drwxr-xr-x 2 root root 4096 Aug 18 15:05 cs
-rwxr-xr-x 1 root root 96 Aug 18 15:06 f1
-rwxr-xr-x 2 root root 40 Aug 18 15:20 f2

4. cd : Change Directory
(A) [ ] $ cd  This command is used to change dir to home directory
Eg.
[sistec@localhost cs ]$ cd 
[sistec@localhost ~ ]$

5. cd .. : This command is used to change dir to one level up.

Abhishek Kumar Singh 0187CS221016 5


Eg.
[sistec@localhost cs ]$ cd .. 
[sistec@localhost engg ]$

6. mkdir –p : This command is used to create a directory hierarchy by using branch


path

Eg.

india

mp up

gwl bpl agra gorakhpur

[ ]$mkdir -p india/mp/gwl
[ ]$mkdir -p india/mp/bpl
[ ]$mkdir -p india/up/agra
[ ]$mkdir -p india/up/gorakhpur

7. rmdir : This command is used to delete empty dir

Syntax :

[ ]$ rmdir <directory Name> 

8. rm -rf : This command is used to delete the whole directory structure.


Syntax:
[ ]$ rm -rf <root Directory> 

Abhishek Kumar Singh 0187CS221016 6


PRACTICAL : 3

Describe the following File management commands with syntax and examples
1. cat >
2. cat
3. more
4. less
5. cat >>
6. cp
7. mv
8. rm

1. cat > : This command is used to create a file in current file system or directory.
Syntax :
[ ]#cat > <file name> 
Eg.
[ ]#cat > f1 
1234567
Abcdefg ------ press crtl + d twice to save file (if cursor is on content line)

[ ]#cat > f2 
1234567
Abcdefg
press crtl + d once to save file (if cursor is on new line)

2. cat : This command is used to read a file.


Syntax :
[ ]#cat <file name> 
Eg.
[ ]#cat f1 
12345678
Abcdefg

3. more : This command is used to view text files in a scrollable manner.

Syntax :
[ ]#more <file name> 
Eg.
[ ]#more f1 
12345678
Abcdefg

4. less : This command is used to view text files .

Syntax :
[ ]#less <file name> 
Eg.
Abhishek Kumar Singh 0187CS221016 7
[ ]#less f1 
12345678
Abcdefg

5. Redirect the file to other files

(A) Redirect one file to another by > .This will overwrite the contents of one file to
another
Syntax :
[ ]#cat fille1 > file2
Eg.
[ ]#cat file1
12345678
[ ]#cat file2
Abcdefg
Then
[ ]# cat file1 > file2 
[ ]#cat file1
12345678
[ ]#cat file2
12345678

(B) Redirect one file to another by >> .This will append the contents of one file to
another
Syntax :
[ ]#cat fille1 >> file2
Eg.
[ ]#cat file1
12345678
[ ]#cat file2
Abcdefg
Then
[ ]# cat file1 >> file2 
[ ]#cat file1
12345678

[ ]#cat file2
Abcdefg
12345678
6. cp : This command is used to copy files. Copy may be file to file or file to directory .

Syntax :
copy content of one file to another file
[ ]#cp <file name1> <file name2>
Eg.
[ ]#cp f1 f2 

Abhishek Kumar Singh 0187CS221016 8


12345678

copy file to a directory


[ ]#cp <file name> <directory name>
Eg.
[ ]#cp f1 sistec 

[ ]#cd sistec  //copy file to a directory


[ sistec]#ls
F1

7. mp : This command is used to move or rename a file or directory.

Syntax :
Rename file
[ ]#mv <old file name> <new file name>
Eg.
[ ]#mv f1 f5 
[ ]#ls
F5

Move a file to a directory


[ ]#mv <file name> <directory name>
Eg.
[ ]#mv f5 sistec 

[ ]#mv sistec  //copy file to a directory


[ sistec]#ls
F5

8. rm : This command is used to remove file.

Syntax :
[ ]#rm <file name>
Eg.
[ ]#rm f1 

To remove file forcely


[ ]#rm -rf f1 

Abhishek Kumar Singh 0187CS221016 9


PRACTICAL : 4

Expain and perform File Permissions on Files and Directories


1. Character Notation
2. Numeric Notation

view Linux file permissions :


The ls command along with its -l (for long listing) option will show you metadata about your Linux
files, including the permissions set on the file.
$ ls -l

drwxr-xr-x. 4 root root 68 Jun 13 20:25 tuned


-rw-r--r--. 1 root root 4017 Feb 24 2022 vimrc

In this example, you see two different listings. The first field of the ls -l output is a group of metadata
that includes the permissions on each file.
 File type: -
 Permission settings: rw-r--r--
 Extended attributes: dot (.)
 User owner: root
 Group owner: root

File permissions control the ability of user and group accounts to view, modify, access, and execute
the contents of the files and directories.

Every file or directory has three levels of ownership:


 User owner (u).
 Group owner (g).
 Others (o).

Each level of ownership can be assigned the following permissions:


 Read (r).
 Write (w).
 Execute (x).

Symbolic notation :

Abhishek Kumar Singh 0187CS221016 10


Using Chmod Command to Change File Permissions
As all Linux users, you will at some point need to modify the permission settings of a file/directory.
The command that executes such tasks is the chmod command.

The basic syntax is:


chmod [permission] [file_name]

There are two ways to define permission:

using symbols (alphanumerical characters)


using the octal notation method
Define File Permission with Symbolic Mode
To specify permission settings using alphanumerical characters, you’ll need to define accessibility for
the user/owner (u), group (g), and others (o).

Type the initial letter for each class, followed by the equal sign (=) and the first letter of the read (r),
write (w) and/or execute (x) privileges.

Abhishek Kumar Singh 0187CS221016 11


To set a file, so it is public for reading, writing, and executing, the command is:
chmod u=rwx,g=rwx,o=rwx [file_name]

To set permission as in the previously mentioned test.txt to be:


• read and write for the user
• read for the members of the group
• read for other users

Use the following command:


chmod u=rw,g=r,o=r test.txt

Numeric Notation
Another way to specify permission is by using the octal/numeric format. This option is faster, as it
requires less typing, although it is not as straightforward as the previous method.
Instead of letters, the octal format represents privileges with numbers:
 r(ead) has the value of 4
 w(rite) has the value of 2
 (e)x(ecute) has the value of 1
 no permission has the value of 0

The privileges are summed up and depicted by one number. Therefore, the possibilities are:
 7 – for read, write, and execute permission
 6 – for read and write privileges
 5 – for read and execute privileges
 4 – for read privileges
As you have to define permission for each category (user, group, owner), the command will include
three (3) numbers (each representing the summation of privileges).

For instance, let’s look at the test.txt file that we symbolically configured with the chmod u=rw,g=r,o=r
test.txt command.

The same permission settings can be defined using the octal format with the command:
chmod 644 test.txt

Abhishek Kumar Singh 0187CS221016 12


PRACTICAL : 5
Describe the following user management commands with syntax and examples
9. useradd
10. passwd
11. cat /etc/passwd
12. useradd –u
13. useradd –g
14. useradd –u –g
15. useradd -c

1. useradd : This command is used to add a new user.

Syntax :

[ ]# useradd <username> 

Eg.

[ ]# useradd sistec 

2. passwd : This command is used to create or change username.

Syntax :
[ ]# passwd <username> 

eg.
[ ]# passwd sistec 
Change password for user sistec.
New UNIX Password :
Retype UNIX Password :
Passwd : all authentication token update successfully.

3. useradd –u : This command is used to add a new user with specified User ID.

Syntax :

[ ]# useradd -u <UserID> <username> 

eg.

[ ]# useradd -u 600 sistec 

4. cat /etc/passwd : This command is used to find list of all users.

[ ]# cat /etc/passwd

Abhishek Kumar Singh 0187CS221016 13


5. useradd –g : This command is used to add a new user with joining a specified Group.

Syntax :

[ ]# useradd -g <GroupID> <username> 

eg.

[ ]# useradd -g 700 CSE 

//To see user information


[ ]# cat /etc/passwd

CSE : x : 601 : 700 : : /home/CSE : /bin/bash

6. useradd -u –g : This command is used to add a new user with specified User ID and
Group.

Syntax :

[ ]# useradd -u <User ID> -g <GroupID> <username> 

eg.

[ ]# useradd -u 650 -g 700 IT 

//To see user information


[ ]# cat /etc/passwd

IT : x : 650 : 700 : : /home/IT : /bin/bash

7. useradd –c : This command is used to add a new user with user comment.

Syntax :

[ ]# useradd -c “User Comment “ <username> 

Abhishek Kumar Singh 0187CS221016 14


eg.

[ ]# useradd -c “CSE Department” 0187cs161001 

//To see user information


[ ]# cat /etc/passwd

0187cs161001 : x : 602 : 701 : CSE Department : /home/CSE : /bin/bash

Abhishek Kumar Singh 0187CS221016 15


PRACTICAL : 6

Describe the following user management commands with syntax and examples
1. passwd
2. usermod -l
3. cat /etc/passwd
4. usermod –u
5. usermod –g
6. usermod –u –g
7. usermod -c
8. userdel
9. userdel -r

1. usermod -l : This command is used to change user login name.

Syntax :

[ ]# usermod -l <old username> <new username> 

Eg.

[ ]# usermod -l sps sistec

2. passwd : This command is used to create or change username.

Syntax :
[ ]# passwd <username> 

eg.
[ ]# passwd sps 
Change password for user sistec.
New UNIX Password :
Retype UNIX Password :
Passwd : all authentication token update successfully.

3. usermod –u : This command is used to modify User ID of a user.

Syntax :

[ ]# usermod -u <UserID> <username> 

eg.

[ ]# useradd -u 800 sps 

//To see user information


[ ]# cat /etc/passwd
Abhishek Kumar Singh 0187CS221016 16
Sps : x : 800 : 700 : : /home/sistec : /bin/bash

4. useradd –g : This command is used to modify Group of a user

Syntax :

[ ]# usermod -g <GroupID> <username> 

eg.

[ ]# usermod -g 800 CSE 

//To see user information


[ ]# cat /etc/passwd

CSE : x : 601 : 800 : : /home/CSE : /bin/bash

5. usermod -u –g : This command is used to modify user ID and Group of a user.

Syntax :

[ ]# usermod -u <User ID> -g <GroupID> <username> 

eg.

[ ]# usermod -u 680 -g 850 IT 

//To see user information


[ ]# cat /etc/passwd

IT : x : 680 : 850 : : /home/IT : /bin/bash

6. usermod –c : This command is used to add a new user with user comment.

Syntax :

[ ]# usermod -c “User Comment “ <username> 

eg.

[ ]# usermod -c “IT Department” 0187cs161001 

//To see user information


[ ]# cat /etc/passwd

Abhishek Kumar Singh 0187CS221016 17


0187cs161001 : x : 602 : 701 : IT Department : /home/CSE : /bin/bash

7. userdel : This command is used to delete a user name without its home directory.

Syntax :

[ ]# userdel <username> 

eg.

[ ]# userdel IT 

8. userdel -r : This command is used to delete a user name with home directory.

Syntax :

[ ]# userdel -r <username> 

eg.

[ ]# userdel -r sps 

Abhishek Kumar Singh 0187CS221016 18


PRACTICAL : 7

Explain and perform group management commands.

There are 2 categories of groups in the Linux operating system i.e. Primary and Secondary groups.
The Primary Group is a group that is automatically generated while creating a user with a unique user
ID simultaneously a group with ID same as the user ID is created and the user gets added to the group
and becomes the first and only member of the group. This group is called the primary group. The
secondary group is a group that can be created separately with the help of commands and we can then
add users to it by changing the group ID of users.

1. Command to Make a group (Secondary Group):


Below command created a group with the name as provided. The group while creation gets a group
ID and we can get to know everything about the group as its name, ID, and the users present in it
in the file “/etc/group”.

groupadd group_name
Example:
groupadd Group1

2. Command to Set the Password for the Group: Below command is used to set the password of
the group. After executing the command we have to enter the new password which we want to assign
to the group. The password has to be given twice for confirmation purposes.

gpasswd group_name
Example:
gpasswd Group1

Command to Display the Group Password File: The below command gives us the password file as
output. The file is present in a form such that no information about the file is open for the viewers.
Instead of this try: “cat /etc/group” to get more information about the groups.

Abhishek Kumar Singh 0187CS221016 19


cat /etc/group

3. Command to Add a User to an Existing Group: Below command is used to add a user to an
existing group. The users which may be present in any primary or secondary group will exit the other
groups and will become the part of this group.

usermod -G group_name username


usermod -G group1 amit

4. Command to Add User to Group Without Removing From Existing Groups: This command is
used to add a user to a new group while preventing him from getting removed from his existing groups.
usermod -aG *group_name *username
Example:
usermod -aG group1 amit

5. Command to Add Multiple Users to a Group at once:

gpasswd -M *username1, *username2, *username3 ...., *usernamen *group_name


Example:
gpasswd -M Person1, Person2, Person3 Group1

6. Command to Delete a User From a Group: Below command is used to delete a user from a group.
The user is then removed from the group though it is still a valid user in the system but it is no more a
part of the group. The user remains part of the groups which it was in and if it was part of no other
group then it will be part of its primary group.

gpasswd -d *username1 *group_name


Example:
gpasswd -d Person1 Group1

7. Command to Delete a Group: Below Command is used to delete the group. The users present in
the group will not be deleted. They will remain as they were, but now they will no more be part of this
group as the group will be deleted.
groupdel *group_name
Example:
groupdel Group1

Abhishek Kumar Singh 0187CS221016 20


PRACTICAL : 8
Explain File System Hierarchy (FSH)
(A) /bin (B) /boot (C) /dev (D) /etc (E) /sbin (F) /lib (G) /media (H) /mnt (I) /opt (J)
/proc (K) /srv (L) /sys (M) /usr (N) /home (O) /var

The Linux File Hierarchy Structure or the Filesystem Hierarchy Standard (FHS) defines the directory
structure and directory contents in Unix-like operating systems. It is maintained by the Linux
Foundation.
In the FHS, all files and directories appear under the root directory /, even if they are stored on different
physical or virtual devices.
Some of these directories only exist on a particular system if certain subsystems, such as the X Window
System, are installed.
Most of these directories exist in all UNIX operating systems and are generally used in much the same
way; however, the descriptions here are those used specifically for the FHS and are not considered
authoritative for platforms other than Linux.

1. / (Root): Primary hierarchy root and root directory of the entire file system hierarchy.
 Every single file and directory starts from the root directory
 The only root user has the right to write under this directory
 /root is the root user’s home directory, which is not the same as /

Abhishek Kumar Singh 0187CS221016 21


2. /bin : Essential command binaries that need to be available in single-user mode; for all users, e.g.,
cat, ls, cp.
 Contains binary executables
 Common linux commands you need to use in single-user modes are located under this
directory.
 Commands used by all the users of the system are located here e.g. ps, ls, ping, grep, cp

3. /boot : Boot loader files, e.g., kernels, initrd.


 Kernel initrd, vmlinux, grub files are located under /boot
 Example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-generic

4. /dev : Essential device files, e.g., /dev/null.


 These include terminal devices, usb, or any device attached to the system.
Example: /dev/tty1, /dev/usbmon0
 dev-linux-directory

5. /etc : Host-specific system-wide configuration files.


 Contains configuration files required by all programs.
 This also contains startup and shutdown shell scripts used to start/stop individual programs.
Example: /etc/resolv.conf, /etc/logrotate.conf.

6. /home : Users’ home directories, containing saved files, personal settings, etc.
 Home directories for all users to store their personal files.
 example: /home/kishlay, /home/kv

7. /lib : Libraries essential for the binaries in /bin/ and /sbin/.


 Library filenames are either ld* or lib*.so.*
 Example: ld-2.11.1.so, libncurses.so.5.7
Abhishek Kumar Singh 0187CS221016 22
8. /media : Mount points for removable media such as CD-ROMs (appeared in FHS-2.3).
 Temporary mount directory for removable devices.
 Examples, /media/cdrom for CD-ROM; /media/floppy for floppy drives; /media/cdrecorder
for CD writer

9. /mnt : Temporarily mounted filesystems.


 Temporary mount directory where sysadmins can mount filesystems.

mnt-linux-directory

10. /opt : Optional application software packages.


 Contains add-on applications from individual vendors.
 Add-on applications should be installed under either /opt/ or /opt/ sub-directory.

11. /sbin : Essential system binaries, e.g., fsck, init, route.


 Just like /bin, /sbin also contains binary executables.
 The linux commands located under this directory are used typically by system administrator,
for system maintenance purpose.
 Example: iptables, reboot, fdisk, ifconfig, swapon

12. /srv : Site-specific data served by this system, such as data and scripts for web servers, data
offered by FTP servers, and repositories for version control systems.
 srv stands for service.
 Contains server specific services related data.
 Example, /srv/cvs contains CVS related data.

13. /tmp : Temporary files. Often not preserved between system reboots, and may be severely size
restricted.
 Directory that contains temporary files created by system and users.
 Files under this directory are deleted when system is rebooted.

14. /usr : Secondary hierarchy for read-only user data; contains the majority of (multi-)user utilities
and applications.
 Contains binaries, libraries, documentation, and source-code for second level programs.
 /usr/bin contains binary files for user programs. If you can’t find a user binary under /bin,
look under /usr/bin. For example: at, awk, cc, less, scp
 /usr/sbin contains binary files for system administrators. If you can’t find a system binary
under /sbin, look under /usr/sbin. For example: atd, cron, sshd, useradd, userdel
 /usr/lib contains libraries for /usr/bin and /usr/sbin
 /usr/local contains users programs that you install from source. For example, when you install
apache from source, it goes under /usr/local/apache2
 /usr/src holds the Linux kernel sources, header-files and documentation.

Abhishek Kumar Singh 0187CS221016 23


15. /proc : Virtual filesystem providing process and kernel information as files. In Linux,
corresponds to a procs mount. Generally automatically generated and populated by the system, on
the fly.
 Contains information about system process.
 This is a pseudo filesystem contains information about running process. For example:
/proc/{pid} directory contains information about the process with that particular pid.
 This is a virtual filesystem with text information about system resources. For example:
/proc/uptime

Abhishek Kumar Singh 0187CS221016 24


PRACTICAL : 9

A. Write a Shell Script to print “Hello Students”.


B. Write a Shell Script to print the following output
Hello
Hi

(A) Write a Shell Script to print "Hello Students"

Step-1 : Create a file using vi editor with extension .sh


[akash@localhost~] $ vi hello.sh
#! /bin/bash

echo "Hello Students"

Step-2 : Set execute permission to the hello.sh as


[akash@localhost~] $ chmod u+x hello.sh

Step-3 : Execute the shell file as


(A). [akash@localhost~] $ ./hello.sh
Hello Students

OR

(B) [akash@localhost~] $ sh hello.sh


Hello Students

Abhishek Kumar Singh 0187CS221016 25


(B) Write a Shell Script to print the following output
Hello
Hi

Step-1 : Create a file using vi editor with extension .sh


[akash@localhost~] $ vi hello.sh
#! /bin/bash

echo -e "Hello\nHi"

Step-2 : Set execute permission to the hello.sh as


[akash@localhost~] $ chmod u+x hello.sh

Step-3 : Execute the shell file as


(A). [akash@localhost~] $ ./hello.sh
Hello
Hi

OR

(B) [akash@localhost~] $ sh hello.sh


Hello
Hi

Abhishek Kumar Singh 0187CS221016 26


PRACTICAL : 10

Write a Shell Script to find sum of two numbers.

#!/bin/bash
clear
echo -n "Enter 1st number: "
read first_number
echo -n "Enter 2nd number: "
read second_number
sum=$(($first_number + $second_number))
echo "Sum of $first_number and $second_number: "$sum

Output:
Enter 1st number:10
Enter 2nd number:20
Sum of 10 and 20 :30

Abhishek Kumar Singh 0187CS221016 27

You might also like