Linux on call · Chapter 26 of 30
Loops
By the end of this chapter you can repeat a command for every file in a folder and for every line of a file, without losing a name to a space. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Shell scripting
- 4 tasks
- Linux
- In the all labs plan
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.
Why you need this
Anjali's new photos for the website came off the camera as .JPG, and the website only shows .jpg:
ls photos
'bun maska.JPG' 'chai 02.JPG' menu-board.png
'chai 01.JPG' 'kitchen team.JPG' rainy-day.JPG
(The quotes are ls warning you: those names have spaces.) Ravi also wants the orders of the last three days counted, file by file, and the menu read out with its prices. Bunty offers to rename the photos by hand. Five mv commands today, three hundred next month, each one a chance to mistype a name with a space in it.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Rename every .JPG in photos/ to .jpg with a loop
Rename the photos with one loop, so you never type a name with a space by hand.
1. Run ls photos. Five names end in .JPG (capital letters). One is menu-board.png.
2. Type a for loop in the terminal. It goes over every .JPG file in photos/.
3. In the loop, move each file to the same name, ending in .jpg (small letters).
4. Keep the variable in double quotes, so names with spaces stay whole.
To pass: photos/ has exactly bun maska.jpg, chai 01.jpg, chai 02.jpg, kitchen team.jpg, menu-board.png and rainy-day.jpg, and nothing else. Each file keeps its contents. You ran a for (or while) loop in the chapter folder, and it worked. A loop with a typing mistake does not count, so type it again.
- Task 2
Write count.sh: one line for each .csv file
Write a script that counts the orders in every .csv file of a folder.
1. Make count.sh in the chapter folder. Line one is the bash shebang.
2. The script gets a folder name as its first argument, like ./count.sh orders.
3. For each .csv file in that folder, print one line: NAME: N orders. NAME is the file name without the folder. N is the number of lines in the file.
4. Make the script executable, then run ./count.sh orders to try it.
To pass: the Check runs count.sh on its own folder. It must print one line for each .csv file, in the order a glob lists them (sorted by name). Files that do not end in .csv are left out. Names with spaces work.
- Task 3
count.sh ends with the total: Total: N orders
Make count.sh also print the total of all the orders.
1. Before the loop, start a total at 0.
2. In the loop, add each file's count to the total.
3. After the loop, print one last line: Total: N orders. N is the total.
To pass: the last line count.sh prints is Total: N orders, with the right sum. For a folder with one 3-line file, only.csv, it prints exactly two lines, only.csv: 3 orders and then Total: 3 orders, and nothing else.
- Task 4
Write prices.sh: ITEM costs Rs PRICE for each line
Write a script that reads a menu file line by line.
1. Make prices.sh in the chapter folder. Line one is the bash shebang.
2. The script gets a file name as its first argument, like ./prices.sh menu.csv.
3. Each line of the file is an item, a comma, then a price. Read the file one line at a time.
4. For each line, print: ITEM costs Rs PRICE. Make the script executable.
To pass: the Check runs prices.sh on its own menu file. It must print one line for each line of the file, in the same order, and nothing else. Item names keep every space, even two spaces in a row: Kesar badam milk,60 gives Kesar badam milk costs Rs 60.
What you will learn
- for: run the same commands once for each word or file in a list.
- Globs and quotes in a loop: why every name arrives whole, and how $(ls) breaks them.
- Cutting a name and adding up: ${f%.txt}, basename and $(( )).
- while read: go through a file line by line, split at commas.
More in Shell scripting
Build "Loops" yourself
Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.