|
75 | 75 |
|
76 | 76 | repl:~/workspace$ bash script.sh |
77 | 77 | Fri |
| 78 | + |
| 79 | +""" |
| 80 | +*** Scope in programmming |
| 81 | + >>>>>> 'Global' : accessible anywhere in the program |
| 82 | + >>>>>> 'Local' : accessible only in ceratain part of the program; like inside a for |
| 83 | + |
| 84 | + *** All vars in Bash are GLOBALly BY default |
| 85 | +""" |
| 86 | + |
| 87 | +""" |
| 88 | +### A percentage calculator |
| 89 | +
|
| 90 | +Instructions |
| 91 | +
|
| 92 | +- Create a function called return_percentage using the function-word method. |
| 93 | +- Create a variable inside the function called percent that divides the first argument fed into the function by the second argument. |
| 94 | +- Return the calculated value by echoing it back. |
| 95 | +- Call the function with the mentioned test values of 456 (the first argument) and 632 (the second argument) and echo the result. |
| 96 | +""" |
| 97 | +# Create a function |
| 98 | +function return_percentage () { |
| 99 | + |
| 100 | + # Calculate the percentage using bc |
| 101 | + percent=$(echo "scale=2; 100 * $1 / $2" | bc) |
| 102 | + |
| 103 | + # Return the calculated percentage |
| 104 | + echo $percent |
| 105 | +} |
| 106 | + |
| 107 | +# Call the function with 456 and 632 and echo the result |
| 108 | +return_test=$(return_percentage 456 632) |
| 109 | +echo "456 out of 632 as a percent is $return_test%" |
| 110 | + |
| 111 | + |
| 112 | +repl:~/workspace$ bash script.sh |
| 113 | +456 out of 632 as a percent is 72.15% |
| 114 | + |
| 115 | +""" |
| 116 | +### Create a function with a local base variable |
| 117 | +
|
| 118 | +An array of numbers you can use for a test of your function would be the daily sales in your organization this week (in thousands): |
| 119 | +
|
| 120 | +14 12 23.5 16 19.34 which should sum to 84.84 |
| 121 | +
|
| 122 | +Instructions |
| 123 | +
|
| 124 | +- Create a function called sum_array and add a base variable (equal to 0) called sum with local scope. You will loop through the array and increment this variable. |
| 125 | +- Create a FOR loop through the ARGV array inside sum_array (hint: This is not $1! but another special array property) and increment sum with each element of the array. |
| 126 | +- Rather than assign to a global variable, echo back the result of your FOR loop summation. |
| 127 | +- Call your function using the test array provided and echo the result. You can capture the results of the function call using the shell-within-a-shell notation. |
| 128 | +""" |
| 129 | +# Create a function with a local base variable |
| 130 | +function sum_array () { |
| 131 | + local sum=0 |
| 132 | + # Loop through, adding to base variable |
| 133 | + for number in "$@" |
| 134 | + do |
| 135 | + sum=$(echo "$sum + $number" | bc) |
| 136 | + done |
| 137 | + # Echo back the result |
| 138 | + echo $sum |
| 139 | + } |
| 140 | +# Call function with array |
| 141 | +test_array=(14 12 23.5 16 19.34) |
| 142 | +total=$(sum_array "${test_array[@]}") |
| 143 | +echo "The total sum of the test array is $total" |
| 144 | + |
| 145 | + |
| 146 | +repl:~/workspace$ bash script.sh |
| 147 | +The total sum of the test array is 84.84 |
0 commit comments