Question
Script 2 Usung nano shell script This script introduces the concept of a loop and the modulo. For this script, you must perform a loop,
Script 2
Usung nano shell script
This script introduces the concept of a loop and the modulo. For this script, you must perform a loop, over a range from 1 to 15, that outputs the remainder of all numbers when divided by 2 (you must also print the operation). For instance, I expect to see the following:
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
4 % 2 = 0
We can use a for loop to iterate over a range of numbers. For instance, if we want to iterate over a range of numbers between 1 and 20, we would use the following:
for i in {1..20}
do
done
The first line (for i in {1..20}) tells the script that each time the loop is executed, the variable i will take on a value between 1 and 20. So, the first time the loop runs, i will be 1, the second time i will be 2, and so on until it hits 20.
The second and last line (do / end) specify that the contents in between will be executed repeatedly until the end case is hit (i = 20).
Finally, lets do a little bit of math. The modulo operator gives us the remainder of a division operation. So, if we divide 10 / 2, the answer is 5 with no remainder. However, 10 / 3 has a remainder of 1. 10/4 has a remainder of 2, and so on.
We wont go into a basic math lesson, but understand that, if a number is divisible by another number, the remainder will be 0.
To find this remainder, we use the modulo, or %, operator. For instance (either work):
[user]$ expr 10 % 2
0
[user]$ echo $[10 % 2]
0
We can use this information to determine if a number is odd or even. If you take the modulo 2 of a number, a remainder of 1 indicates that the initial number is odd, and a remainder of 0 indicates if it is even.
Again, save your script file as
please send the answer with photos
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started