Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1: To create a shell script for execution in UNIX system, you need to follo

ID: 3677074 • Letter: P

Question

Part 1:

To create a shell script for execution in UNIX system, you need to follow three steps:

(1) Create a shell script file with an editor (vim,emacs,nona...)

e.g. $vim GetStarted.sh

(2) Type your script in the editor and remember that the first line should declare which kind of shell this script is based on.

e.g. #!/bin/bash

(3) Save the file and change permission type so is can be executable.

e.g. $chmod +x GetStarted.sh

(4) In the current directory, execute the shell script.

e.g. $./GetStarted.sh

Now write a bash script (GetStarted.sh) to accept your first name as an argument and display the success information. The output could be like this.

$./GetStarted.sh Bing
Congratulations, Bing! You already known how to get started with shell script.

Part 2:

Here is the definition of Fibonacci numbers (from Wikipedia):

In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence:

0, 1, 1, 2, 3, 5, 8, 13 ...

By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

Fn = Fn-1 + Fn-2 with seed values F0 =0,F1 =1.

Write a bash script (fib_lastname.sh) calculating Fibonacci number. It takes one argument n (n >= 0). Do not try numbers greater than 13, which may not work correctly due to number range limitation. The output looks like:

$./fib_li.sh 8 fib 8 = 21

$./fib_li.sh 10 fib 10=55

Explanation / Answer

Part1:

GetStarted.sh

#!/bin/bash

echo 'Congratulation,' $1'!'

//

Output:

:~/Desktop$ ./GetStarted.sh Bing
Congratulation, Bing!

Part2:

#!/bin/bash
function fib(){
    if [ $1 -le 0 ]; then
        echo 0
    elif [ $1 -eq 1 ]; then
        echo 1
    else
        echo $[`fib $[$1-2]` + `fib $[$1 - 1]` ]
    fi

}

echo 'fib' $1 '=' `fib $1`

//

Output:

:~/Desktop$ ./fib_alex.sh 8
fib 8 = 21