1. Write a script in Bourne Shell that prints out the following table. Use a while loop to do so. \t can be used with echo to create a tab space.
N | S |
1 | 1 |
2 | 4 |
3 | 9 |
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.
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\t\t\t|S\t|"
echo "---------------------------------"
while [ $i -le 3 ]
do
k=` expr $i \* $i`;
echo -e "|$i\t\t\t|$k\t|"
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
Get Answers For Free
Most questions answered within 1 hours.