A shell script to automate file tasks

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

A shell script to automate file tasks

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. Which script line copies report.txt into the backup folder?
A mv report.txt backup/
B cp report.txt backup/
3. A backup script must keep the original file where it is. Which command should it use?
A cp
B mv
4. Which line stores a folder name in a variable in a shell script?
A dest = backup
B dest=backup
5. Which line uses the variable dest to copy a file?
A cp data.txt $dest
B cp data.txt dest

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

shell script

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
#!/bin/bash __ notes.txt backup/notes.txt
2.
#!/bin/bash __ report.txt archive/report.txt
3.
DEST=backup cp notes.txt "$____/notes.txt"
4.
for f in *.txt; do cp "$f" backup/ ____
5.
chmod __ backup.sh ./backup.sh

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A #!/bin/bash
B !#/bin/bash
C echo "Backup done"
Question 2
A #!/bin/bash
B SRC=/home/sam/docs
C DEST=/home/sam/backup
D cp SRC/*.txt DEST
Question 3
A NAME = report.txt
B cp $NAME backup/
C echo Copied $NAME
Question 4
A #!/bin/bash
B for f in *.txt; then
C cp $f backup/
D done
Question 5
A #!/bin/bash
B mv archive/ old.txt
C echo Moved old.txt

Answer Key

A shell script to automate file tasks
Section A: Quick Questions
Q1
A — A text file of commands that run in order each time the script is run
Q2
B — cp report.txt backup/
Q3
A — cp
Q4
B — dest=backup
Q5
A — cp data.txt $dest
Section B: Match the Pairs
Q1
cp report.txt backup/ → Copies report.txt into the backup folder when this line of the script runs
Q2
chmod +x backup.sh → Makes the script file executable so it can be run as a program
Q3
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/
Q4
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
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
#!/bin/bash cp notes.txt backup/notes.txt
Q2
#!/bin/bash mv report.txt archive/report.txt
Q3
DEST=backup cp notes.txt "$DEST/notes.txt"
Q4
for f in *.txt; do cp "$f" backup/ done
Q5
chmod +x backup.sh ./backup.sh
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
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.
Q5
B — mv takes the source first and the destination second. The arguments are reversed here. It should be mv old.txt archive/.