Write a bash script called minor4A.sh that prints a trapezoid with the top-base
ID: 3754571 • Letter: W
Question
Write a bash script called minor4A.sh that prints a trapezoid with the top-base and height dimensions given from two parameters passed as command line arguments. The syntax is as follows: minor4A.sh top-base height The mandatory integral parameter top-base must be greater than 1 and the height must be greater than 0, but can be any reasonable integral value (as long as the terminal supports it). If any of the parameters are less than the required value, you are to display a meaningful error message and terminate the program. If no, two few, or too many arguments, are given, you are to display an appropriate usage statement, such as usage: ./minor4A.sh top-base height, and terminate the program. In this program, you are to draw the appropriate sized trapezoid with the character on all four comers (with leading spaces for the top-base, the correct number of- characters at the top and bottom rows between the'+' characters at the corners, and the '/" or characters with the correct number of spaces in between on the intermediate lines. SAMPLE OUTPUT (user input shown in bold): $ /minor4A.sh usage: ./minor4A·sh top-base height $ /minor4A.sh 5 3 4 usage: ./minor4A.sh top-base height S /minor4A.sh 1 2 error: top-base () must be > 1 S /minor4A.sh 2 0 error: height (0) must be > 0 S ./minor4A.sh 4 2 S /minor4A.sh 2 1 $ ./minor4A.sh 5 3 $ /minor4A.sh 8 4Explanation / Answer
The required bash shell script is as follows;
#!bin/bash
if [$# -lt 3]
then
echo "usage: ./minor4A.sh top-base height"
elif [$# -gt 3]
then
echo "usage: ./minor4A.sh top-base height"
else
if[$1 -le 1]
then
echo "error: top-base (1) must be > 1"
elif [$2 -le 0]
then
echo "error: height (0) must be > 1"
else
for i in 1 .. $2+1
do
echo " "
done
echo "+"
if [$1 -eq 2]
then
echo "+ "
elif [$1 -gt 2]
then
for i in 1 .. $1-2
do
echo "-"
done
echo "+ "
fi
for i in 1 .. $2
do
for j in 1 .. $2-$i+1
do
echo " "
done
echo "/"
$first =$j
for k in $j .. $2+$i-2+$1+1
do
echo " "
done
echo "\"
echo " "
$last = k
done
echo "+"
for i in $first .. $last+1
do
echo "-"
done
echo "+ "
fi
fi
//The corresponding c program is also attached with this. But the initial conditions are not implemented //in c. Like comparision of no of arguments etc.. please find the corresponding c program
#include<stdio.h>
int main()
{
int x,i;
int y,j,k,first,last;
char s='/';
//printf(" Enter top_base and height ");
scanf("%d%d",&x,&y);
for(i=1;i<=y+1;i++) ///giving spacing for first line
printf(" ");
printf("+"); //printing first line
if(x==2)
printf("+ ");
else if(x>2)
{
for(i=1;i<=x-2;i++)
printf("-");
printf("+ ");
}
for(i=1;i<=y;i++)
{
for(j=1;j<=y-i+1;j++)
printf(" ");
printf("%c",s);
first=j;
for(k=j;k<=y+i-2+x+1;k++)
printf(" ");
printf("\");
printf(" ");
last=k;
}
printf("+");
for(i=first;i<=last+1;i++)
printf("-");
printf("+ ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.