Class 35 of 37 • Practice & Exam Prep \t\t\t\t

Linux Programming Practical Lab Programs (BCA & CS)

Welcome to the complete practical lab manual for Linux Programming. Below are university-tested solutions for practical examinations in BCA, B.Tech, and MCA curricula.

Program 1: Find the Largest of Three Numbers


#!/bin/bash
# Find greatest among 3 numbers entered by user

echo -n "Enter three numbers: "
read n1 n2 n3

if [ $n1 -ge $n2 ] && [ $n1 -ge $n3 ]; then
    max=$n1
elif [ $n2 -ge $n1 ] && [ $n2 -ge $n3 ]; then
    max=$n2
else
    max=$n3
fi

echo "The greatest number is: $max"
							

Program 2: Check Whether a Year is a Leap Year


#!/bin/bash
# A year is a leap year if divisible by 400 OR (divisible by 4 and not by 100)

echo -n "Enter year (YYYY): "
read year

if [ $((year % 400)) -eq 0 ] || [ $((year % 4)) -eq 0 -a $((year % 100)) -ne 0 ]; then
    echo "$year is a Leap Year!"
else
    echo "$year is NOT a Leap Year."
fi
							

Program 3: Check If a Number is Prime


#!/bin/bash
# Check Prime Number

echo -n "Enter integer: "
read num

if [ $num -le 1 ]; then
    echo "$num is neither prime nor composite."
    exit 0
fi

is_prime=1
for (( i=2; i*i<=num; 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
							

Program 4: Generate Fibonacci Series up to N Terms


#!/bin/bash
# Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, ...

echo -n "Enter number of terms N: "
read n

a=0
b=1

echo "Fibonacci Series up to $n terms:"
for (( i=0; i<n; i++ ))
do
    echo -n "$a "
    fn=$(( a + b ))
    a=$b
    b=$fn
done
echo ""
							

Program 5: Check String Palindrome


#!/bin/bash
# Palindrome string checker (e.g., 'madam', 'racecar')

echo -n "Enter a word/string: "
read str

# Reverse string using rev utility
rev_str=$(echo "$str" | rev)

if [ "$str" = "$rev_str" ]; then
    echo "'$str' is a PALINDROME!"
else
    echo "'$str' is NOT a palindrome."
fi
							

Program 6: Count Lines, Words, and Characters in a File


#!/bin/bash
# File stats analyzer

echo -n "Enter file path to analyze: "
read filepath

if [ ! -f "$filepath" ]; then
    echo "Error: File does not exist!"
    exit 1
fi

lines=$(wc -l < "$filepath")
words=$(wc -w < "$filepath")
chars=$(wc -m < "$filepath")

echo "--------------------------------"
echo "Analysis for: $filepath"
echo "Total Lines      : $lines"
echo "Total Words      : $words"
echo "Total Characters : $chars"
echo "--------------------------------"
							

Program 7: C System Call - Parent and Child with fork()


/*
 * fork_lab.c - University Practical Exam Program
 * Compile: gcc fork_lab.c -o fork_lab
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
    pid_t pid = fork();

    if (pid < 0) {
        printf("Error: Fork creation failed!\n");
        return 1;
    } else if (pid == 0) {
        printf("[CHILD PROCESS]  My PID = %d, Parent PPID = %d\n", getpid(), getppid());
    } else {
        wait(NULL); // Parent waits for child
        printf("[PARENT PROCESS] My PID = %d, Child PID = %d\n", getpid(), pid);
    }

    return 0;
}
							

Program 8: C System Call - Half-Duplex IPC Pipe


/*
 * pipe_lab.c - Communication between parent and child via pipe()
 * Compile: gcc pipe_lab.c -o pipe_lab
 */

#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main() {
    int pfd[2];
    char inbuf[50];
    char msg[] = "Veewom Practical Exam IPC";

    pipe(pfd);

    if (fork() == 0) {
        // Child reads from pfd[0]
        close(pfd[1]);
        read(pfd[0], inbuf, sizeof(inbuf));
        printf("Child received message: %s\n", inbuf);
        close(pfd[0]);
    } else {
        // Parent writes to pfd[1]
        close(pfd[0]);
        write(pfd[1], msg, strlen(msg) + 1);
        close(pfd[1]);
    }

    return 0;
}
							

Frequently Asked Questions (FAQ)

You can install WSL 2 (Windows Subsystem for Linux) by opening PowerShell as Administrator and typing wsl --install. This gives you a native Ubuntu terminal inside Windows where you can run Bash scripts and compile C programs with GCC.

Run your script in debug trace mode by prepending bash -x script.sh. Bash will print every variable assignment, expression evaluation, and command as it executes.

Next Step in Your Linux Learning Path:

Prepare for university viva exams and technical job interviews in Class 36: 50+ Questions & Answers.

Continue to Class 36: 50+ Viva & Interview Q&A → Previous: ← GCC & Make | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!