Description: Basic shell script demonstrating a while loop using WSL on Windows 11.
| Attribute | Value |
|---|---|
| Machine | ASUS VIVO BOOK, Mouse Windows 11 |
| Linux (WSL) | Ubuntu 24.04.1 LTS |
| IDE | VS Code |
uname -a |
Linux asus-oda 5.15.167.4-microsoft-standard-WSL2 1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux |
pwd |
/home/miura |
whoami |
miura |
which bash |
/usr/bin/bash |
| Current Directory | /home/miura/WECM100 |
#!/usr/bin/bash
i=0
while [ $i -ne 10 ]
do
((i++))
printf "{\"count\":%d}\n" $i
done
#!/usr/bin/bash): Correct and necessary for executing the script.i=0 initializes the counter variable.while Loop: The while loop iterates until i equals 10.((i++)) increments the counter in each iteration.printf "{\"count\":%d}\n" $i generates JSON-like output, which is useful for structured data.[ $i -ne 10 ] ensures the loop terminates correctly.while loop.i is common for counters, more descriptive names can be used in larger scripts.for loop could be more concise.Commands:
$ chmod +x base02.sh
$ ./base02.sh
Result:
{"count":1}
{"count":2}
{"count":3}
{"count":4}
{"count":5}
{"count":6}
{"count":7}
{"count":8}
{"count":9}
{"count":10}