Subject: Unix Operating Systems 1) Write a script using for statement(s) to prin
ID: 3850628 • Letter: S
Question
Subject: Unix Operating Systems
1) Write a script using for statement(s) to printout the following?
******
*****
****
***
**
*
**
***
****
*****
******
2) Write Script called maxmin to find out the biggest and the smallest numbers from given five numbers. Numbers are supplies as command line arguments. Print error message if sufficient arguments are not supplied.
This script works as follows $ maxmin 17 12 44 5 19
Max = 55
Min = 5
$ maxmin 300 29 277 888 99
Max = 888
Min = 29
3) Write a script called srm (super remove) that deletes the specified filename(s) and asks for confirmation before deletion.
Q.4. Write Script calculate, using case statement to perform basic math operation as
follows
+ addition
- subtraction
x multiplication
/ division
This script works as follows
$ calculate 20 / 4,
Result = 5
$ calculate 12 - 2,
Result = 10
$ calculate 7 x 5,
Result = 35
$ calculate 18 + 12,
Result = 30
Also this script should check for sufficient command line arguments
Explanation / Answer
1. The script for printing the pattern is as follows:
#!/bin/bash
Triangle()
{
input=$1;
#Loop for printing rows
for((i=input;i>=1;--i))
do
#Loop for printing spaces
for((j=1;j<=i;++j))
do
echo -ne "*";
done
#Echo for new line
echo;
done
}
InTriangle()
{
input=$1;
#Loop for printing rows
for((i=1;i<=input;++i))
do
#Loop for printing spaces
for((j=1;j<=i;++j))
do
echo -ne "*";
done
#Echo for new line
echo;
done
}
#calling function
#change number according to your need
Triangle 5
InTriangle 5
2. The script for finding the largest and the smallest number is as follows:
maxOrmin.sh
#!/bin/bash
echo "Please enter the size of the array"
read input
#Take input from the user
for((uu=0;uu<input;uu++))
do
echo " Enter the $((uu+1)) numbers: "
read numbers[$uu]
done
#Print the entered numbers
echo "Numbers Entered are as follows: "
for((uu=0;uu<input;uu++))
do
echo ${numbers[$uu]}
done
minimum=${numbers[0]}
maximum=${numbers[0]}
for((uu=0;uu<input;uu++))
do
#For finding Smallest number
if [ ${numbers[$uu]} -lt $minimum ]; then
minimum=${numbers[$uu]}
#For finding Largest number
elif [ ${numbers[$uu]} -gt $maximum ]; then
maximum=${numbers[$uu]}
fi
done
#Print the smallest and the largest number in the array
echo "Minimum number in an array is $minimum"
echo "Maximum number in an array is $maximum"
3.The script for super removal is as follows:
#!/bin/bash
read
ip=$FEEDBCK
echo "Confirm removal of $ip ?"
read
if [ "$FEEDBCK" != "no" ]
then
rm -f $ip
fi
4. The script for calculating basic maths operation is as follows:
op=0
uu="Y"
echo "Please enter a number: "
read ip
echo "Please enter another number to continue: "
read ip1
while [ $uu = "Y" ]
do
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "Please enter a valid selection: "
read choice
case $choice in
1)op=`expr $ip + $ip1`
echo "Sum ="$op;;
2)op=`expr $ip - $ip1`
echo "Sub = "$op;;
3)op=`expr $ip * $ip1`
echo "Mul = "$op;;
4)op=`expr $ip / $ip1`
echo "Div = "$op;;
*)echo "Invalid choice";;
esac
echo "Wish to continue?"
read uu
if [ $uu != "Y" ]
then
exit
fi
Hence, these are all the script programs that were needed by you and hence, all are working and fit.
Please rate this answer if it helped.....Thankyou
Hope it helps.....