Week 12 Usp Lab Programs
Week 12 Usp Lab Programs
To write a shell script that takes a number as input from the user and displays the reverse of
the given number.
Description
This shell script reads a number from the user and reverses its digits using mathematical
operations.
1. Looping Structure:
o A while loop ensures the reversal logic runs until all digits are processed (i.e.,
the input number becomes 0).
2. Arithmetic Operations:
o Modulus (%): Extracts the last digit of the number.
o Multiplication and Addition: Used to build the reversed number.
o Division (/): Removes the last digit from the original number after every
iteration.
3. Conditional Execution:
o The loop continues only if the input number is greater than 0 ([ $number -
gt 0 ]).
Code
#!/bin/ksh
1. Reading Input:
o The user is prompted to enter a number, which is stored in the variable
number.
2. Initializing Variables:
o The reverse variable is initialized to 0 to store the reversed number.
3. Using a While Loop:
o The while loop runs until the input number becomes 0.
o In each iteration:
The remainder is extracted using the modulus operation (number %
10).
The reversed number is built by multiplying the current reverse by 10
and adding the remainder.
The last digit is removed from the original number using division
(number / 10).
4. Displaying Output:
o Once the loop completes, the reversed number is printed to the screen.
Output Examples
Example Run 1
$ ./12_1_reverseanumber.ksh
Enter a number:
234
Reversed Number is: 432
Example Run 2
$ ./12_1_reverseanumber.ksh
Enter a number:
4219
Reversed Number is: 9124
Inferences
To write an interactive shell program that offers the user multiple options to copy, remove,
rename, or link files.
Description
This script provides a menu-based interface for performing basic file handling operations.
The operations include:
1. Copying a file
2. Removing a file
3. Renaming a file
4. Creating a symbolic link
5. Exiting the menu
The user selects an option from the menu, and the corresponding file operation is executed
using the appropriate shell commands like cp, rm, mv, and ln -s. The script keeps running
until the user chooses to exit by selecting Option 5.
Code
#!/bin/ksh
# Infinite loop to keep displaying the menu until the user chooses to exit
while true; do
# Display the menu options
echo "File Handling Menu"
echo "1. Copy File"
echo "2. Remove File"
echo "3. Rename File"
echo "4. Create Symbolic Link"
echo "5. Exit"
echo "Enter your choice: "
Example Run 1
$ ./12_2_switch_case.ksh
File Handling Menu
1. Copy File
2. Remove File
3. Rename File
4. Create Symbolic Link
5. Exit
Enter your choice:
1
Enter the source file name:
a.txt
Enter the destination file name:
b.txt
File copied successfully.
Example Run 2
Example Run 3
Example Run 4
Example Run 5
Inferences
This interactive script offers a simple and effective way to manage files through a menu-
driven interface. It demonstrates the usage of basic file handling commands in KornShell
scripting (KSH) and ensures that each operation is executed based on the user’s input. The
script also showcases control flow using case statements and infinite loops.
To write a shell script that displays the Fibonacci series up to a given number of terms.
Description
The Fibonacci series is a sequence of numbers where each term is the sum of the two
preceding ones, starting from 0 and 1.
The script takes the number of terms as input from the user and displays the corresponding
Fibonacci sequence using a for loop.
#!/bin/ksh
1. Input Handling:
o The user is prompted to enter the number of terms to generate using:
2. Variable Initialization:
o a and b are initialized to 0 and 1, which are the first two terms of the
Fibonacci series.
3. Using the for Loop:
o The for loop runs from 0 to the entered number of terms (terms - 1).
Example: If the user enters 10, the loop runs 10 times.
o At each iteration:
1. The current value of a is printed.
2. fn stores the sum of a and b (the next Fibonacci term).
3. a is updated to the value of b.
4. b is updated to the value of fn.
4. Output Display:
o After the loop ends, the complete Fibonacci series is displayed on the
terminal.
Output Examples
Example 1
$ ./12_3_fibonacci.ksh
Enter the number of terms for the Fibonacci series:
10
Fibonacci Series:
0
1
1
2
3
5
8
13
21
34
Example 2
$ ./12_3_fibonacci.ksh
Enter the number of terms for the Fibonacci series:
5
Fibonacci Series:
0
1
1
2
3
Key Concepts
Inferences:
This shell script provides a practical demonstration of generating the Fibonacci series
using simple arithmetic operations and loop constructs in KornShell scripting.
To write a shell script that checks whether the given string is a palindrome.
Description
A palindrome is a string that reads the same backward as forward (e.g., "madam", "level").
The script takes a string as a command-line argument and determines if the input string is a
palindrome by reversing it and comparing it with the original. If both are identical, the script
prints that the string is a palindrome; otherwise, it prints that the string is not a palindrome.
Code
#!/bin/ksh
if [ -z "$1" ]; then
input=$1
o The rev command is used to reverse the input string and store it in reverse:
Output Examples
$ ./12_4_palindrome.ksh
Usage: ./12_4_palindrome.ksh <string>
$ ./12_4_palindrome.ksh madam
madam is a palindrome.
$ ./12_4_palindrome.ksh mvgr
mvgr is not a palindrome.
1. Command-line Arguments:
o The first argument provided to the script is accessed using $1.
2. Using rev Command:
o The rev command reverses the input string.
Example: echo madam | rev outputs madam.
3. String Comparison:
o The = operator is used inside the if statement to check if the original and
reversed strings are identical.
4. Exit Status:
o exit 1 is used to exit the script if no input is provided, indicating an error.
Inferences:
This script demonstrates how to use command-line arguments, string reversal, and
conditional checks in KornShell scripting. It helps students understand basic string
manipulation and comparison logic in a shell environment.
#!/bin/ksh
echo "Enter a number:" # Prompt user to enter a number
read number # Read the input from the user
factorial=1 # Initialize factorial to 1
# Loop from 1 to the input number
for (( i=1; i<=number; i++ ))
do
factorial=$((factorial * i)) # Multiply factorial with the current value of i
done
Output Examples
Example 1
$ ./12_5_factorial.ksh
Enter a number:
10
Factorial of 10 is 3628800.
Example 2
$ ./12_5_factorial.ksh
Enter a number:
5
Factorial of 5 is 120.
Explanation of Key Concepts
1. Looping with for:
o The for loop helps in repeatedly multiplying numbers from 1 to the input
number.
2. Arithmetic Operations:
o The * operator performs multiplication.
o (( ... )) is used for arithmetic expansion within the loop.
3. Handling Edge Cases:
o If the input number is 0, the script still works correctly since 0! = 1.
Inferences:
This shell script demonstrates how to calculate the factorial of a number using a for loop in
KornShell scripting. It provides hands-on experience with loops, arithmetic operations,
and basic input handling.