← WA Computer Science ATAR Unit 1

A shell script to automate file tasks

A text file of shell commands, such as cp, mv or rm, that run in order each time the script is run, so file tasks like backups can be automated.

Understanding A shell script to automate file tasks in WA Computer Science ATAR Unit 1

What it does: The two commands sit on separate lines and run top to bottom each time the script runs, so the copy happens first and the move second. The for loop sets f to each .txt file in turn, so one cp command backs up every matching file. A shell script holds a series of shell commands. Running it executes them one after another, so file tasks can be automated. cp copies a file and leaves the original in place. mv moves it, so the original is no longer in its old location. In a shell script there must be no spaces around the = sign when assigning a variable.

Common mistakes: The first line of a shell script must be the shebang #!/bin/bash, with # first and then !. Written as !#, the system cannot tell which shell should run the script. A variable's value is read by putting $ in front of its name. Without the $, cp looks for a folder literally named SRC. It should be cp $SRC/*.txt $DEST.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 315–470) and word search (Modules 100–470).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What is a shell script?
A text file of commands that run in order each time the script is runA program that can only run once and then deletes itself
Matches
folder = backup cp a.txt $folder/
↓
Spaces around the = in a variable assignment. The shell treats folder as a command, so the variable is never set. It should be folder=backup.
Applying
Meanings
Term
shell script
Meaning
A text file of shell commands, such as cp, mv or rm, that run in order each time the script is run, so file tasks like backups can be automated.
Practice
SRC=~/Documents
Testing
Gaps
notes.txt backup/notes.txt
Check Answer
Spots
1#!/bin/bash
2!#/bin/bash

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Lion's Mane Jellyfish' badge — Set Operator, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What is a shell script?
A. A text file of commands that run in order each time the script is run ✓
B. A program that can only run once and then deletes itself
A shell script holds a series of shell commands. Running it executes them one after another, so file tasks can be automated.
2. Which script line copies report.txt into the backup folder?
A. mv report.txt backup/
B. cp report.txt backup/ ✓
cp copies a file and leaves the original in place. mv moves it, so the original is no longer in its old location.
3. A backup script must keep the original file where it is. Which command should it use?
A. cp ✓
B. mv
cp makes a copy and leaves the original untouched. mv would remove the file from its original folder.
4. Which line stores a folder name in a variable in a shell script?
A. dest = backup
B. dest=backup ✓
In a shell script there must be no spaces around the = sign when assigning a variable.
5. Which line uses the variable dest to copy a file?
A. cp data.txt $dest ✓
B. cp data.txt dest
A $ before the variable name gets its value. Without it, the shell treats dest as plain text, not the variable.

Match the Pairs

cp report.txt backup/ → Copies report.txt into the backup folder when this line of the script runs
chmod +x backup.sh → Makes the script file executable so it can be run as a program
A script runs its commands in order, so a script can copy a file and then move another file → cp notes.txt backup/ mv draft.txt archive/
A loop in a script repeats the same command for every file that matches a pattern → for f in *.txt; do cp "$f" backup/; done
folder = backup cp a.txt $folder/ → Spaces around the = in a variable assignment. The shell treats folder as a command, so the variable is never set. It should be folder=backup.
cp a.txt backup/ ./backup.sh → The script has no execute permission, so ./backup.sh fails with a permission denied error. Run chmod +x backup.sh first.

Key Term

shell script
A text file of shell commands, such as cp, mv or rm, that run in order each time the script is run, so file tasks like backups can be automated.

Fill in the Blanks

1. #!/bin/bash cp notes.txt backup/notes.txt
Hint: Type the command that copies a file, so the script keeps the original and makes a duplicate.
2. #!/bin/bash mv report.txt archive/report.txt
Hint: Type the command that moves a file to a new folder, so the original no longer stays where it was.
3. DEST=backup cp notes.txt "$DEST/notes.txt"
Hint: Type the name of the variable that was set on the first line, so its stored value is used here.
4. for f in *.txt; do cp "$f" backup/ done
Hint: Type the keyword that closes the loop block after the commands that run for each file.
5. chmod +x backup.sh ./backup.sh
Hint: Type the chmod option that adds execute permission so the script file can be run.

Spot the Error

Question 1
A #!/bin/bash
B !#/bin/bash
C echo "Backup done"
Line B — The first line of a shell script must be the shebang #!/bin/bash, with # first and then !. Written as !#, the system cannot tell which shell should run the script.
Question 2
A #!/bin/bash
B SRC=/home/sam/docs
C DEST=/home/sam/backup
D cp SRC/*.txt DEST
Line D — A variable's value is read by putting $ in front of its name. Without the $, cp looks for a folder literally named SRC. It should be cp $SRC/*.txt $DEST.
Question 3
A NAME = report.txt
B cp $NAME backup/
C echo Copied $NAME
Line A — Shell variable assignments must not have spaces around the = sign. With spaces, the shell treats NAME as a command. It should be NAME=report.txt.
Question 4
A #!/bin/bash
B for f in *.txt; then
C cp $f backup/
D done
Line B — A shell for loop opens its body with the keyword do, not then. (then belongs to if statements.) It should be for f in *.txt; do.
Question 5
A #!/bin/bash
B mv archive/ old.txt
C echo Moved old.txt
Line B — mv takes the source first and the destination second. The arguments are reversed here. It should be mv old.txt archive/.

Student with a class code? Join your class →