Lab Exercise – 1
AIM – Create a list of few basic commands used in Linux
Software Used: Linux
Theory:
Linux is an open-source, Unix-like operating system kernel created by Linus Torvalds in 1991. It is widely used
in servers, desktops, mobile devices (like Android), and embedded systems.
• Kernel: The core of Linux, responsible for managing hardware resources, processes, memory, and system
calls.
• Open Source: Distributed under the GNU General Public License (GPL), allowing users to freely use,
modify, and distribute the software.
• Distributions (Distros): Complete operating systems built around the Linux kernel, such as Ubuntu,
Fedora, Debian, Red Hat, and Arch Linux.
• File System: Follows a hierarchical structure, starting from the root directory /.
• Shell: A command-line interface (e.g., Bash, Zsh) used to interact with the system through commands.
• Security & Stability: Known for high security, multitasking, multi-user support, and stability, making it
popular for servers and critical systems.
• Applications: Supports programming, networking, system administration, and everyday use (browsers,
office suites, etc.).
Some basic Linux commands are as follows: -
1) ls – This command is used to list all the files in the current directory
2) pwd – This command is used to print the current working directory
3) cd – This command is used to change the directory
4) touch [Link] – this command is used to create a text file named “[Link]”
5) mkdir – This command is used to create a directory
6) rm [Link] – this command is used to delete the file created
7) rmdir – This command is used to delete an empty directory
8) cp src dest – This command is used to transfer files from source to destination
9) mv src dest – This command is used to move files from the source directory to the destination directory
10) cat file1 – This command is used to display the contents of the file.
11) More/less – These commands are used as used as “pagers” to view the contents of text files page by
page
12) Passwd – This command is used to change the password of the current user
13) gcc file.c -o output – This command is used to compile a “C” code and create an output file.
Lab Exercise – 2
Aim: Write a script to find the greatest of three numbers (numbers passed as command line parameters)
Software used: Linux
Theory:
In Linux shell scripting, command line arguments allow users to pass values to a script when executing it. These
values are accessed using positional parameters such as $1, $2, and $3 for the first three arguments. To find the
greatest of three numbers, the script accepts three values as command line parameters and compares them using
conditional statements like if, elif, and else. Based on these comparisons, the script determines which number is
the largest and displays it as the output. This demonstrates the use of command line arguments along with
decision-making in shell scripting.
Source Code:
#!/bin/bash
read -p "Enter first number: " a
read -p "Enter second number: " b
read -p "Enter third number: " c
if [ $a -ge $b ] && [ $a -ge $c ]; then
greatest=$a
elif [ $b -ge $a ] && [ $b -ge $c ]; then
greatest=$b
else
greatest=$c
fi
echo "The Greatest Number Is: $greatest"
Output:
Lab Exercise – 3 (i)
Aim: Write a script to check whether a script is even or odd
Software used: Linux
Theory:
In Linux shell scripting, we can check whether a number is even or odd using the modulus operator %. A number
is considered even if it is completely divisible by 2, otherwise it is odd. The script takes a number either from the
user or as a command line argument and performs the calculation using arithmetic expansion $(( )). By applying
conditional statements like if and else, the script checks the remainder when the number is divided by 2 and
displays whether the number is even or odd. This demonstrates the use of arithmetic operations and decision-
making in shell scripting.
Source Code:
#!/bin/bash
read -p "Enter a number: " num
if [ $((num % 2)) -eq 0 ]; then
echo "$num is Even"
else
echo "$num is Odd"
fi
Output:
Lab Exercise – 3 (ii)
Aim: Write a script to check whether a given number is prime or not
Software Used: Linux
Theory:
In Linux shell scripting, a number can be checked for primality by testing whether it has any divisors other than
1 and itself. A prime number is defined as a number greater than 1 that is divisible only by 1 and itself, while
any number with additional factors is composite. The script generally accepts a number from the user and then
uses a loop to divide the number by all integers from 2 up to half of the number or its square root. If any
division gives a remainder of zero, the number is not prime; otherwise, it is prime. This script demonstrates the
use of loops, conditional statements, and arithmetic operations in shell scripting.
Source Code:
#!/bin/bash
read -p "Enter a number: " num
if [ $num -le 1 ]; then
echo "$num is not a Prime Number"
exit 0
fi
is_prime=1
for ((i=2; i<=num/2; i++))
do
if [ $((num % i)) -eq 0 ]; then
is_prime=0
break
fi
done
if [ $is_prime -eq 1 ]; then
echo "$num is a Prime Number"
else
echo "$num is Not a Prime Number"
fi
Output:
Lab Exercise - 3 (iii)
Aim: Write a script to check whether a given input is number or a string
Software Used: Linux
Theory:
In Linux shell scripting, it is possible to check whether a given input is a number or a string by using conditional
expressions and pattern matching. When a user enters input, the script can test it against a numeric pattern using regular
expressions or comparison operators inside [[ ]]. If the input contains only digits, it is identified as a number; otherwise, it
is treated as a string. This type of script demonstrates the use of user input handling, string comparison, and pattern
matching in shell scripting, and is useful for basic input validation tasks.
Source Code:
#!/bin/bash
read -p "Enter something: " input
if [[ $input =~ ^[0-9]+$ ]]; then
echo "The input is a Number"
else
echo "The input is a String"
fi
Output:
Lab Exercise - 3 (iv)
Aim: Write a script to compute no. of characters and words in each line of given file
Software Used: Linux
Theory:
A script to compute the number of characters and words in each line of a file helps analyze text content efficiently.
It works by reading the file line by line, counting the total characters in each line and splitting the line into words
to determine the word count. The results are then displayed for each line, showing both the character and word
counts. Such a script demonstrates basic concepts of file handling, string manipulation, and iteration, and is useful
in text processing and data analysis tasks.
Source Code:
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: $0 filename"
exit 1
fi
file=$1
line_no=1
while IFS= read -r line
do
char_count=${#line}
word_count=$(echo $line | wc -w)
echo "Line $line_no: Characters = $char_count, Words = $word_count"
((line_no++))
done < "$file"
Output:
Lab Exercise - 3 (v)
Aim: Write a script to calculate the average of n numbers
Software Used: Linux
Theory:
A script to calculate the average of n numbers is used to determine the mean value of a set of numbers provided
by the user. The script works by first accepting the total count of numbers and then reading each number
sequentially. It adds all the numbers to compute their sum and then divides this sum by the total count to get the
average. This type of script demonstrates fundamental programming concepts such as input handling, loops,
arithmetic operations, and output formatting, and is commonly used in mathematical computations and data
analysis.
Source Code:
#!/bin/bash
read -p "Enter the number of elements: " n
sum=0
for (( i=1; i<=n; i++ ))
do
read -p "Enter number $i: " num
sum=$((sum + num))
done
average=$(echo "scale=2; $sum / $n" | bc)
echo "The average of $n numbers is: $average"
Output: