PRACTICAL FILE
ON
OS & LINUX PROGRAMMING
COURSE CODE: 301
SUBMITTED TO SUBMITTED BY:
Dr Sumit Chauhan Name: Priyansh Kumar
Roll No: 00315102021
Class: B.C.A 5TH SEMESTER
1
[Link]. TITLE PAGE
NO.
1. Write a shell script using ‘case’ 3-4
statement. This offers following
choices to the user
i) sort file contents in ascending
order.
ii) count the no. of lines
iii) converts the contents into
capital letters
iv) Exit
2. Take the input from the user & 5
report whether it is file or a
directory or something else.
3. Write a script which reads your 6
name, surname & year of
birth as command line
arguments and gives the output
like.
My name is : ………………
My Surname is : ………………
My YOD is : ………………
4. Write a script that takes a no. ‘n’ 7
& a word as command line
input. Then it prints the
word ‘n’ times.
5. Write a program to sort a list of N 8
integers.
6. Write a script which displays the 9-10
options as follows
i) Display time
ii) Display day, month & year
iii) Display day of week
7. Write a shell program to print the 11
factorial of an integer.
8. Write a shell program to print the 12
Fibonacci series.
9. Write a shell program to construct 13
a multiplication table from 1 to 10.
10. Write a shell program to print the 14
reverse of a number.
11. Write a shell program to find out 15-16
the HCF of 2 numbers.
12. Write a shell program to display 17
the table of a number.
2
Question 1: Write a shell script using a ‘case’ statement.
This offers the following choices to the user
i) Sort file contents in ascending order.
ii) count the no. of lines
iii) converts the contents into capital letters
iv) Exit
According to the user’s choice, ask the file name and
perform the desired action.
CODE:
while true; do
echo "Menu:"
echo "1) Sort file contents in ascending order."
echo "2) Count the number of lines."
echo "3) Convert the contents into capital letters."
echo "4) Exit"
read -p "Enter your choice (1-4): " choice
case $choice in
1)
read -p "Enter file name: " filename
sort "$filename" ;;
2)
read -p "Enter file name: " filename
lines=$(wc -l < "$filename")
echo "Number of lines: $lines" ;;
3)
read -p "Enter file name: " filename
awk '{print toupper($0)}' "$filename" ;;
4)
echo "Exiting the script."
exit ;;
*)
echo "Invalid choice. Please enter a number between 1 and
4." ;;
esac
3
read -p "Do you want to perform another action? (y/n): " continue
if [ "$continue" != "y" ]; then
echo "Exiting the script."
break
fi
done
OUTPUT
4
Question 2: Take the input from the user & report
whether it is file or a directory or something else.
CODE
read -p "Enter a file or directory path: " user_input
if [ -e "$user_input" ]; then
if [ -f "$user_input" ]; then
echo "$user_input is a regular file."
elif [ -d "$user_input" ]; then
echo "$user_input is a directory."
else
echo "$user_input is something else (not a regular file or
directory)."
fi
else
echo "Path does not exist: $user_input"
fi
OUTPUT
5
QUESTION 3: Write a script which reads your name,
surname & year of birth as command line arguments
and gives the output.
My name is : ………………
My Surname is : ………………
My YOD is : ………………
CODE
name=$1
surname=$2
yob=$3
echo "My name is : $name"
echo "My Surname is : $surname"
echo "My YOD is : $yob"
OUTPUT
6
QUESTION 4 : Write a script that takes a no. ‘n’ & a
word as command line input. Then it prints the
word ‘n’ times.
CODE
n=$1
word=$2
if ! [[ "$n" =~ ^[1-9][0-9]*$ ]]; then
echo "Error: Please provide a positive integer as the first
argument."
exit 1
fi
for ((i = 1; i <= n; i++)); do
echo "$word"
done
OUTPUT
7
QUESTION 5 : Write a program to sort a list of N integers.
CODE
if [ "$#" -eq 0 ]; then
echo "Usage: $0 <integer1> <integer2> ..."
exit 1
fi
sorted_numbers=$(printf "%s\n" "$@" | sort -n)
echo "Sorted numbers: $sorted_numbers"
OUTPUT
8
QUESTION 6: Write a script which displays the options as
follows
i) Display time
ii) Display day, month & year
iii) Display day of week
According to user’s choice it displays the required output.
CODE
while true; do
echo "Menu:"
echo "i) Display time"
echo "ii) Display day, month & year"
echo "iii) Display day of week"
echo "q) Quit"
read -p "Enter your choice (i/ii/iii/q): " choice
case $choice in
i)
echo "Current time: $(date +%H:%M:%S)" ;;
ii)
echo "Current date: $(date '+%A, %B %d, %Y')" ;;
iii)
echo "Day of the week: $(date +%A)" ;;
q)
echo "Exiting the script."
exit ;;
*)
echo "Invalid choice. Please enter i, ii, iii, or q." ;;
9
esac
read -p "Do you want to perform another action? (y/n): "
continue
if [ "$continue" != "y" ]; then
echo "Exiting the script."
break
fi
done
OUTPUT
10
QUESTION 7: Write a shell program to print the factorial
of an integer.
CODE
read -p "Enter the number : " number
fact=1
while [ $number -gt 1 ]
do
fact=$((fact * number)) #fact = fact * num
number=$((number - 1)) #num = num - 1
done
echo "Result:" $fact
OUTPUT
11
QUESTION 8: Write a shell program to print the Fibonacci
series.
CODE
N=6
a=0
b=1
echo "The Fibonacci series is : "
for (( i=0; i<N; i++ ))
do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
OUTPUT
12
QUESTION 9: Write a shell program to construct a
multiplication table from 1 to 10.
CODE
echo "Multiplication Table from 1 to 10"
for ((i=1; i<=10; i++))
do
# Inner loop for columns (multipliers)
for ((j=1; j<=10; j++))
do
product=$((i * j))
echo -n -e "$product\t"
done
echo ""
done
OUTPUT
13
QUESTION 10: Write a shell program to print the reverse
of a number.
CODE
echo "Enter a number:"
read number
temp_number=$number
reversed_number=0
while [ $number -gt 0 ]
do
digit=$(( $number % 10 ))
reversed_number=$(( $reversed_number * 10 + $digit ))
number=$(( $number / 10 ))
done
echo "The reverse of $temp_number is: $reversed_number"
OUTPUT
14
QUESTION 11: Write a shell program to find out the HCF
of 2 numbers.
CODE
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
function find_hcf() {
local a=$1
local b=$2
while [ $b -ne 0 ]; do
local remainder=$(( $a % $b ))
a=$b
b=$remainder
done
echo $a
}
hcf=$(find_hcf $num1 $num2)
echo "The HCF of $num1 and $num2 is: $hcf"
15
OUTPUT
16
QUESTION 12: Write a shell program to display the table
of a number.
CODE
echo "Enter a number:"
read base_number
echo "Multiplication Table for $base_number"
for ((i = 1; i <= 10; i++)); do
result=$((base_number * i))
echo "$base_number x $i = $result"
done
OUTPUT
17