Welcome to Westonci.ca, your one-stop destination for finding answers to all your questions. Join our expert community now! Experience the convenience of finding accurate answers to your questions from knowledgeable professionals on our platform. Our platform offers a seamless experience for finding reliable answers from a network of knowledgeable professionals.

Write a script called pow.sh that is located in your workspace directory to calculate the power of two integer numbers (e.g. a ^ b); where a and b are passed as parameters from the command line (e.g. ./pow.sh 2 3). The result should be echoed to the screen as a single integer (e.g. 8).

Sagot :

Answer:

#! /usr/bin/bash

echo $[$1**$2]

Explanation:

(a) The first line of the code is very important as it tells the interpreter where the bash shell is located on the executing computer.

To get this on your computer, simply:

1. open your command line

2. type the following command: which bash

This will return the location where the bash shell is. In this case, it returned

/usr/bin/bash

(b) The second line is where the actual arithmetic takes place.

The variables $1 and $2 in the square bracket are the first and second arguments passed from the command line. The ** operator raises the left operand (in this case $1) to the power of the second operand (in this case $2).

Then, the echo statement prints out the result.

(c) Save this code in a file named as pow.sh then run it on the command line.

For example, if the following is run on the command line: ./pow.sh 2 3

Then,

the first argument which is 2 is stored in $1 and;

the second argument which is 3 is stored in $2.

The arithmetic operator (**) then performs the arithmetic 2^3 which gives 8.