Real Unix
Real Unix
PAGE: 1
ASSIGNMENT -1
Problem Statement:
Write a shell script to display the Fibonacci series up to ‘n’ numbers where ‘n’
is taken as input from the user.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the first two values of Fibonacci series i.e. 0 and 1 in ‘a’ and
‘b’.
Step 3: Take the no. of terms from the user and set it in ‘n’.
Step 4: Print the value of ‘a’ and ‘b’.
Step 5: Repeat the following steps until ‘i’= 3 to ‘n’.
a) Evaluate c=a + b.
b) Print the value of ‘c’.
c) Evaluate a=b.
d) Evaluate b=c.
e) Evaluate i=i+1.
f) End the loop.
Step 6: End the program.
Source Code:
a=0
b=1
read -p "Enter the number of terms: " n
echo "The Fibonacci series is as follows :"
echo -n " $a $b"
for i in $(seq 3 $n)
do
c=$(($a + $b))
echo -n " $c"
a=$b
b=$c
done
PAGE: 2
Output:
Discussion:
(1) The script assumes that the user will input a valid positive integer. It
would be a good idea to add input validation to ensure that the user
enters a non-negative integer.
(2) For very large values of n, the Fibonacci numbers can become large, and
the script may not handle them well due to limitations in Bash
arithmetic.
(3) The script uses a basic iterative approach. For a large number of terms,
this might not be the most efficient solution. The algorithm can be
optimized if needed.
(4) The script doesn't include robust error-handling mechanisms. If
unexpected issues occur during execution, the script might not handle
them gracefully.
PAGE: 3
ASSIGNMENT -2
Problem Statement:
Write a shell script to find prime numbers in a given range where the range is
taken as input from the user.
Algorithm:
Step 1: Start the program.
Step 2: Take the starting and ending number from the user and initialize them
in ‘a’ and ‘b’ respectively.
Step 3: If the starting number (a) is less than 1 then end the program otherwise,
continue the next steps accordingly. Step 4: Repeat the following steps until
‘i’= ‘a’ to ‘b’.
a) Initialize k=0.
b) Execute Step 5.
c) If k=0, then only print the value of ‘i’.
d) Evaluate i=i+1.
e) End the loop.
Step 5: Repeat the following steps until ‘j’=2 to (‘i’/2).
a) If only (‘i’%’j’)= 0, then Initialize k=1 and break out the of the loop.
b) Evaluate j=+1.
c) End the loop.
Step 6: End the program.
Source Code:
echo -n " Enter the starting number:"
read a
echo -n " Enter the ending number:"
read b
if [ $a -le 1 ]
then
echo "The starting must be greater than 1"
else
echo "The prime numbers between $a and $b are as follows:"
for i in $( seq $a $b )
do
k=0
PAGE: 4
for j in $( seq 2 $(expr $i / 2) )
do
if [ $(expr $i % $j) -eq 0 ]
then
k=1
break
fi
done
if [ $k -eq 0 ]
then
echo -n " $i"
fi
done
fi
Output:
Discussion:
(1) This script uses a basic method to check for prime numbers. For larger
ranges, it might not be the most efficient.
(2) The script assumes that the user enters valid numerical inputs. Adding
input validation checks would make the script more robust.
(3) The script does not handle cases where the user inputs non-integer values
or negative numbers. Adding checks for these cases would improve the script.
(4) The script simply prints prime numbers to the console. Depending on the
use case, we can format the output differently, store it in a file, or use it in
another way.
PAGE: 5
ASSIGNMENT- 3
Problem Statement:
Write a shell script to find whether a number is Krishnamurthy number where
the number is taken as input from the user.
Algorithm:
Step 1: Start the program.
Step 2: Take the number to be checked from the user and initialize it in
‘num’.
Source Code:
read -p " Enter the number to be checked: "
num real=$num
sum=0
while [ $num -ne 0 ] do
fact=1
rem=$(($num % 10))
num=$(($num / 10)) for i
in $(seq 1 $rem)
PAGE: 6
do
fact=$(($fact * $i))
done
sum=$(($sum + $fact))
done
if [ $real -eq $sum ] then
echo “ The number $real is a Krishnamurthy number.”
else
echo “ The number $real is a not Krishnamurthy number.”
fi
Output:
Discussion:
(1) A while loop is used to iterate through each digit of the input
number, and a nested for loop calculates the factorial of each digit.
(3) This script assumes that the user will input a valid integer. Error
handling can be added for cases where non-integer or invalid input is
provided.
PAGE: 7
ASSIGNMENT -4
Problem Statement:
Write a shell script to find the sum of series 1/1 + 1/4+ 1/9+….+1/n^2
where ‘n’ is taken as input from the user. \
Algorithm:
Step 1: Start the program.
Step 2: Take ‘n’ number of terms.
Step 3: Print the series.
Step 4: Repeat the following steps until ‘i’= 1 to ‘n’.
a) Evaluate ‘pow’= i * i.
b) Evaluate ‘res’ =1/pow.
c) Evaluate ‘sum’=sum + res.
d) Evaluate i=i + i..
e) End the loop.
Step 5: Print the value of ‘sum’.
Step 6: End the program.
Source Code:
echo -n "Enter the number of terms: "
read n
sum=0
echo "The series is : "
for i in $(seq 1 $n)
do
pow=$(echo "scale=5; $i * $i" | bc)
res=$(echo "scale=5; 1 / $pow" | bc)
sum=$(echo "scale=4; $sum + $res" | bc)
if [ $i -eq 1 ]
then
echo -n " 1/$pow "
else
echo -n " + 1/$pow "
fi
done
echo " The sum of the given series is $sum."
PAGE: 8
Output:
Discussion:
(1) (1)The script assumes the user will enter a valid positive integer for
'n'. Additional input validation could be added to ensure a valid input
is provided.
(2) Performance wise if the value of 'n' is large it might take quite a while
for this script to finish running since the loop it uses is iterative, in
nature. However there are algorithms that are more efficient when it
comes to calculating the sum of this series.
(3) The script utilizes the bc command, for performing floating point
calculations; however it may not guarantee precision. To achieve
accuracy, alternative tools or programming languages might be more
appropriate.
PAGE: 9
ASSIGNMENT- 5
Problem Statement:
Write a shell script to perform basic calculator operation.
Algorithm:
Step 1: Start the program.
Step 2: Take the operand from the user and initialize their value in ‘a’ and
‘b’.
Step 3: Repeat the following steps until while equals to 1.
a) Display a menu of basic calculator operations (addition,
subtraction, multiplication, division and exiting).
b) Perform the selected operations based on user’s choice.
• Evaluate sum= a + b, when 1 is entered.
• Evaluate sum= a - b, when 2 is entered.
• Evaluate sum= a * b, when 3 is entered.
• Evaluate sum= a / b, when 1 is entered.
• Exit the process, when 5 is entered.
c) Print the value of the operation.
Step 4: End the program.
Source Code:
read -p " Enter the first number : " a
read -p " Enter the second number : " b
while [ 1 ]
do
echo " The options are as follows :
1 for addition
2 for subtraction
3 for multiplication
4 for division
5 for exiting"
read -p " Enter the option according to your choice: " ch
case $ch in
1)sum=$(echo "$a + $b" | bc)
echo "The result of addition of $a and $b is $sum." ;;
2)sub=$(echo "$a - $b" | bc)
echo "The result of subtraction of $a and $b is $sub." ;;
3)mul=$(echo "$a * $b" | bc)
echo "The result of multiplication of $a and $b is $mul." ;;
4)div=$(echo "scale=5;$a / $b" | bc)
echo "The result of division of $a and $b is $div." ;;
5) echo "Thank you"
PAGE: 10
exit ;;
*) echo " Invalid Choice!!!!" ;;
esac
done
Output:
Discussion:
(1) It is important to validate the users input to ensure that the numbers
entered are valid and that a valid choice is made. Incorporating input
validation is crucial, in order to handle any errors, such as entering
numeric values or dividing, by zero.
(2) The script utilizes the be command, for performing floating point
calculations; however it may not guarantee precision. To achieve
accuracy, alternative tools or programming languages might be more
appropriate.
(3) There is an option in the given code which helps the user to exit the
program, otherwise there will be a infinite looping in the program.
PAGE: 11
ASSIGNMENT-6
Problem Statement:
Write a shell script to check whether the string which is taken as input is
palindrome or not.
Algorithm:
Step 1: Start the program.
Step 2: Take a string from the user.
Step 3: Repeat following steps until i = 0 to (size of the word) – 1.
a) Concatenate each character (having position valued ‘i’) from the
original string to the beginning of a new string.
b) Evaluate i = i + 1
c) End the loop.
Step 4: Check whether the new string is equal to original string. If strings
match then execute Step-5, otherwise execute Step-6.
Step 5: Print that the given string is palindrome.
Step 6: Print that the given string is not palindrome.
Step 7: End the program
.
Source Code:
read -p " Enter the string to be checked palindrome: " word
echo " The given word is $word."
for(( i=0;i<${#word};i++ ))
do
rword=${word:$i:1}$rword
done
echo " The reverse word is $rword."
if [ "$word" = "$rword" ]
then
echo " The given string is palindrome."
else
echo " The given string is not palindrome."
fi
PAGE: 12
Output:
Discussion:
(1) The script iterates over each character to reverse the string. While
this approach is straightforward, it may not be the most efficient for very
long strings. Considerations for optimization could be made.
(2) Reversing the string by iterating through each character is a
common approach. Alternatively, tools like ‘rev’ command in Unix-like
systems can achieve the same result more concisely.
(3) The script uses a simple conditional check for palindrome. While
effective, more advanced techniques (e.g., ignoring case or considering
white spaces) might be considered based on specific requirements.
(4) The script assumes a bash environment due to its syntax. If
portability across different shells is a concern, adjustments may be
needed.
PAGE: 13
ASSIGNMENT-7
Problem Statement:
Write a shell script to search an element in an array where element is taken
as input from user by using the binary search method.
Algorithm:
Step 1: Start the program.
Step 2: Take size of the array from the user and also take the element to be
searched.
Step 3: Take the elements to be searched from the user and put them in the
array and display the given array.
Step 4: Initialize l=0 and r= (size of the array -1)
Step 5: Repeat following steps while ‘l’ is less than or equals to ‘r’.
a) Evaluate ‘mid’ = (l + r) / 2.
b) If element at the position ‘mid’ = searched element, print the
position and exit the program.
c) Or if, element at the position ‘mid’ < searched element,
evaluate
l= (mid + 1).
d) Or if, element at the position ‘mid’ > searched element,
evaluate
r= (mid - 1).
Step 6: If the element doesn't found, display that the given aray does not
contain the required element.
Step 7: End the program.
Source Code:
read -p " Enter the number of terms to be taken: " n
echo " Enter the elements in sorted way: "
for(( i=0;i<n;i++))
do
read -p " Enter the element at position $(($i + 1)) : " arr[$i]
done
echo " The given array is ${arr[@]}."
read -p " Enter the element to be searched: " find
l=0
r=$(($n - 1))
while [ "$l" -le "$r" ]
PAGE: 14
do
mid=$(( $(($l + $r)) / 2))
if [ "${arr[$mid]}" -eq "$find" ]
then
echo " Enter the element at position $(($mid + 1)) ."
exit 0
elif [ "${arr[$mid]}" -lt "$find" ]
then
l=$(( $mid + 1 ))
else
r=$(( $mid - 1 ))
fi
done
echo " Given Element not found in the array."
Output:
Discussion:
(1) The success of binary search relies on a sorted array. The code
assumes that the user provides the elements in ascending order. It's
important to communicate and enforce this requirement for accurate
results.
(2) Upon finding the element, the script exits with ‘exit 0 ‘. This
ensures that the script terminates immediately, saving computational
resources once the search is successful.
PAGE: 15
ASSIGNMENT -8
Problem Statement:
Write a shell script to sort an array by using bubble sort method where
array is taken as input from the user.
Algorithm:
Step 1: Start the program.
Step 2: Take the size of the array and then the elements of the array.
Step 3: Display the original array.
Step 4: Repeat following steps until i = 0 to (size of the array -1)
a) Execute Step-5.
b) Evaluate i = i + 1.
c) End the loop.
Step 5: Repeat following steps until j = 0 to less than (size of the array – i -
1).
a) If element at position ‘j’ > element at position ‘j + 1’.
b) Swap the elements at position ‘j’ and ‘j + 1’.
c) End the loop.
Step 6: Display the modified array
Step 7: End the program.
Source Code:
read -p " Enter the number of elements to be taken : " n
echo " Enter the elements to be sorted:-"
for(( i=0;i<n;i++ ))
do
read -p " Enter the element at position $(($i + 1)) : " arr[$i]
done
echo " The given array is ${arr[@]}."
for((i=0;i<n-1;i++))
do
for((j=0;j<n-i-1;j++))
do
if [ ${arr[j]} -gt ${arr[j+1]} ]
then
temp=${arr[j]}
arr[j]=${arr[j+1]}
arr[j+1]=$temp
PAGE: 16
fi
done
done
echo " The sorted array is ${arr[@]}."
Output:
Discussion:
(1) Bubble Sort is a simple sorting algorithm with a time
complexity of O(n^2). While effective for small datasets, it may not
be the most efficient for larger arrays. Considerations for
optimization could be made.
(2) The code assumes that the user inputs the elements to be sorted.
Validating user input (e.g., checking for non-numeric entries) may
enhance the robustness of the code.
(3) The inner loop compares adjacent elements and swaps them if
they are in the wrong order. This process is repeated until the array
is sorted.
PAGE: 17
ASSIGNMENT- 9
Problem Statement:
Write a shell script to print the lines of a text files in reverse order (i.e. 1st
line of the text file is to be displayed last, 2nd line of the text file is to be
displayed as 2nd last line and so on).
Algorithm:
Step 1: Start the program.
Step 2: Take the file name from the user and search it whether the file is
present or not.
Step 3: If the file is present,
a) Then display the original content of the file.
b) Then the ‘tac’ function is used to reverse the lines.
c) Then print the changed file content.
Step 4: Otherwise, print that the file doesn’t exist.
Step 5: End the program.
Source Code:
read -p " Enter the nameof the file to be used : " name
if [ -f"$name" ]
then
echo " The original content in the given file is : "
cat "$name"
echo ""
echo " The lines in the file have been reversed"
echo " The changed content is :"
tac "$name"
else
echo " ERROR.The given file named '$name' doesn't exists. "
fi
PAGE: 18
Output:
Discussion:
(1) Before attempting to read or reverse the file, the script checks if
the file exists. This is a good practice to avoid errors and handle non-
existent files.
(2) The use of ‘cat’ function to display the original content and ‘tac’
function to reverse lines is straightforward and effective.
(3) The code maintains consistent indentation and structure,
contributing to its readability.
PAGE: 19
ASSIGNMENT -10
Problem Statement:
Write a shell script to delete all the lines containing the word "UNIX" from
a given text file. If the file is not found then give error message.
Algorithm:
Step 1: Start the program.
Step 2: Take the file name from the user and search it whether the file is
present or not.
Step 3: If the file is present,
a) Then display the original content of the file.
b) Then the ‘grep’ function is used to remove the lines containing
the word ‘UNIX’.
c) Then print the changed file content.
Step 4: Otherwise, print that the file doesn’t exist.
Step 5: End the program.
Source Code:
read -p " Enter the file name to be checked : " name
if [ -f "$name" ]
then
echo " The original content in the given file is : "
cat "$name"
echo ""
echo " The lines contain word 'UNIX' has been removed."
echo " The changed content is :"
grep -v "UNIX" "$name"
else
echo " ERROR.The given file named "$name" doesn't exists. "
fi
PAGE: 20
Output:
Discussion:
(1) The script checks whether the specified file exists before
attempting to process it. This helps avoid errors when the file is
not present. Displaying the original content of the file before
modifications provides clarity to the user about the changes made.
(2) The script efficiently uses the ‘grep’ command with the ‘-v’
option to exclude lines containing the word "UNIX" from the
original content.
(3) The script does not directly modify the original file; instead, it
displays the changed content. If desired, the user can redirect
the output to a new file or modify the original file accordingly.
PAGE: 21