1. Write a script in Bourne Shell that prints out the following table. Use a whi
ID: 3799495 • Letter: 1
Question
1. Write a script in Bourne Shell that prints out the following table. Use a while loop to do so. can be used with echo to create a tab space.
2. Write a Bourne Shell script that uses a function that you write called cube (you need to write the body of the function in the script) that accepts a variable whose value is cubed and prints our this cubed value. The main part of the script will ask the user for an integer to be cubed and then call this function cube and giving it the integer to be cubed.
Explanation / Answer
Script:
#!/bin/bash
cube()
{
val=$n
val=`expr $n * $n * $n`
echo "Given number is $n";
echo "Cubed value is: $val";
}
i=1;
k=1;
echo "---------------------------------"
echo -e "|N |S |"
echo "---------------------------------"
while [ $i -le 3 ]
do
k=` expr $i * $i`;
echo -e "|$i |$k |"
i=`expr $i + 1`
echo "---------------------------------"
done
n=0;
echo -e "Enter number:";
read n
cube $n
Output:
---------------------------------
|N |S |
---------------------------------
|1 |1 |
---------------------------------
|2 |4 |
---------------------------------
|3 |9 |
---------------------------------
Enter number:
5
Given number is 5
Cubed value is: 125
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.