Copy/paste your modified script from step 3 here( The one without the infinite l
ID: 3829414 • Letter: C
Question
Copy/paste your modified script from step 3 here( The one without the infinite loop. )the script on both directories again, piping the results to wc-l. How many total files were reported from each directory? Run the script on both directories using the following notation (on a single command line): echo ./script 4. sh /usr/sbin I wc l ./script4.sh /usr/bin I wc -1 Did it work? If not, keep trying until you can get it to work. 3. Create a new script, script5.sh, with the following code. When done, change its permissions to be 755. in/bash value 5 while t value -gt 0 1; do value (value+1)) echo $value done Run the script. It does not stop. Why not? You have an infinite loop. Type ctrl+c to exit the program. What is causing the infinite loop? See if you can figure it out. (Here's a hint: countdown) Modify the script and rerun it. Keep trying until you get it figured out. When done, explain how you solved the problem.
Explanation / Answer
3. The loop while [ $value -gt 0 ] is infinite because the condition is always true.
Explanation :
1. Initial value of value =5.
2. Then it checks whether value > 0 , yes the condition is true.
3. Then the next statement inside the loop is increment value by 1, thus value becomes 6
4. Again the while condition is checked for value of variable value greater than than 0, and is true
5. Every time the value is incremented by 1 and thus it is always greater than 0.
The Modification needed to make the loop finite is:
Change the statement value=$((value+1)) to value=$((value-1))
So we get
#!/bin/bash
value=5
while [ $value -gt 0 ]; do
value=$((value-1))
echo $value
done
Here is the output :
4
3
2
1
0
Use chmod -R 755 (pathe of your scriptfile) to change the permission of your script file