Module 7 — Exercises
These exercises follow the lessons in order. Each one builds on the previous. Work through them in sequence, and try to write each script from memory before looking at the lessons for reference.
Exercise 1 — Your First Script
Section titled “Exercise 1 — Your First Script”Write a script called hello.sh that:
- Contains a correct shebang line
- Prints your name
- Prints the current date using the
datecommand - Prints the message:
Ready to do bioinformatics.
Make it executable and run it.
Expected output (your name and date will differ):
AlexWed 23 Apr 2026 10:00:00Ready to do bioinformatics.Exercise 2 — Variables and basename
Section titled “Exercise 2 — Variables and basename”Write a script called show_sample.sh that:
- Stores the path
Training/short_reads/paired/SRR1553607_2.fastqin a variable calledINPUT - Uses
basenameand$()to extract just the sample name — without the directory path or the.fastqextension — and stores it in a variable calledSAMPLE - Prints both the full path and the clean sample name
Expected output:
Full path: Training/short_reads/paired/SRR1553607_2.fastqSample name: SRR1553607_2Exercise 3 — A Simple Loop
Section titled “Exercise 3 — A Simple Loop”Write a script called list_samples.sh that:
- Loops over every
.fastqfile inTraining/short_reads/paired/ - For each file, prints a line in this format:
Found sample: SRR1553607_1Found sample: SRR1553607_2Use basename inside the loop to get the clean sample name.
Exercise 4 — Log Files
Section titled “Exercise 4 — Log Files”Write a script called log_samples.sh that:
- Stores the log filename
sample_list.txtin a variable calledLOG - Uses
>to write the lineSample listinto the log file at the start - Loops over every
.fastqfile inTraining/short_reads/paired/ - For each file, uses
>>to append the clean sample name to the log - Prints
Log saved to: sample_list.txtto the terminal when finished
After running the script, check the contents of sample_list.txt. It should contain:
Sample listSRR1553607_1SRR1553607_2Exercise 5 — Capturing All Output with exec
Section titled “Exercise 5 — Capturing All Output with exec”Write a script called exec_log.sh that:
- Defines a variable
LOG_FILE="full_run.log" - Uses
exec > >(tee $LOG_FILE) 2>&1to capture everything — stdout and stderr — to the terminal and the log file simultaneously - Prints
Run started - Loops over every
.fastqfile inTraining/short_reads/paired/, printingProcessing: [sample_name]for each one - Prints
Run finished
After running the script:
- Confirm the messages appeared in the terminal
- Confirm
full_run.logcontains the same messages
Then try this variation: change the exec line to exec 2> errors.log and run it again. Notice that normal output still prints to the terminal — only errors would be captured in errors.log.
Exercise 6 — Build Your Own Pipeline Script
Section titled “Exercise 6 — Build Your Own Pipeline Script”Write a script called my_qc_pipeline.sh that:
- Defines
DATA_DIR,OUTPUT_DIR, andLOG_FILEas variables at the top - Uses
exec > >(tee $LOG_FILE) 2>&1so all output is captured automatically - Creates the output directory using
mkdir -p - Loops over every
.fastqfile inDATA_DIR - For each file:
- Prints
Quality checking: [sample_name] - Runs
fastqc $FILE --outdir $OUTPUT_DIR
- Prints
- Prints
Pipeline complete.when the loop finishes
Write this script from scratch. Use Lesson 6 as a reference only if you are stuck.