Lesson 3 - Scripting and Permissions
Learning Objectives
Section titled “Learning Objectives”- Create simple bash scripts with variables and loops.
- Edit scripts using
nano. - Run scripts using relative paths from different directories.
- Interpret file permissions with
ls -l. - Make a script executable with
chmod.
Conceptual Overview
Section titled “Conceptual Overview”A bash script is a plain text file containing a sequence of commands. Scripts help you repeat analyses, reduce mistakes, and keep a record of what you did. The first line (shebang) tells Linux which interpreter to use. File permissions control who can read, write, or execute a file.
In bioinformatics, scripts often summarize datasets, run tools in loops, or organize outputs into consistent folders.
Worked Examples
Section titled “Worked Examples”1) Inspect permissions
Section titled “1) Inspect permissions”ls -l uses the -l option for a long listing with permissions, size, and timestamps.
ls -l Training/short_reads/pairedOutput:
total 109600drwxrwxrwx 1 bodeoni bodeoni 4096 Jan 16 16:45 SRR1553607-rwxrwxrwx 1 bodeoni bodeoni 56113290 Jan 16 11:34 SRR1553607_1.fastq-rwxrwxrwx 1 bodeoni bodeoni 56113290 Jan 16 11:34 SRR1553607_2.fastq2) Create a script with nano
Section titled “2) Create a script with nano”nano opens a simple text editor in the terminal.
nano Module_1_Linux/summarize_fastq.shPaste the script below into nano (save with Ctrl+O, exit with Ctrl+X):
This script uses wc -l to count lines and head -n 1 to show the first header line.
#!/usr/bin/env bash
FILES=( "Training/short_reads/paired/SRR1553607_1.fastq" "Training/long_reads/barcode57.fastq")
for f in "${FILES[@]}"; do echo "FILE: $f" echo "LINES: $(wc -l < "$f")" head -n 1 "$f" echo ""done3) Run the script with bash
Section titled “3) Run the script with bash”bash runs the script using the Bash interpreter.
bash Module_1_Linux/summarize_fastq.shOutput:
FILE: Training/short_reads/paired/SRR1553607_1.fastqLINES: 813780@SRR1553607.1 1 length=101
FILE: Training/long_reads/barcode57.fastqLINES: 34268@5832c8b6-696e-46cd-be8d-73b789952b4e st:Z:2024-06-27T02:52:17.771+00:00 RG:Z:fba2136ff67b57066e5c7e23383eba9e075ff2b2_dna_r10.4.1_e8.2_400bps_sup@v4.3.04) Make the script executable
Section titled “4) Make the script executable”chmod +x adds the execute (x) permission.
No output appears when it succeeds.
chmod +x Module_1_Linux/summarize_fastq.shNow you can run it directly using a relative path.
./Module_1_Linux/summarize_fastq.shExercises
Section titled “Exercises”- Create a script named
fastq_summary.shinModule_1_Linux/that loops over all FASTQ files inTraining/long_readsand prints:
- filename
- line count
- first header line
-
Run the script from the
Module_1_Linux/directory using a relative path. -
Make your script executable and run it without calling
bashexplicitly. -
Create a
results/folder insideModule_1_Linux/and redirect the script output toresults/long_reads_summary.txt. -
Challenge: Extend your script to include
Training/short_reads/paired/SRR1553607_1.fastqandTraining/short_reads/paired/SRR1553607_2.fastq, and print an estimated read count for each (lines / 4).
Solutions
Section titled “Solutions”Solution 1
Section titled “Solution 1”nano opens the file so you can paste the script.
nano Module_1_Linux/fastq_summary.shScript content:
#!/usr/bin/env bash
for f in Training/long_reads/*.fastq; do echo "FILE: $f" echo "LINES: $(wc -l < "$f")" head -n 1 "$f" echo ""doneSolution 2
Section titled “Solution 2”./ runs a script from the current directory.
cd Module_1_Linuxbash ./fastq_summary.shSolution 3
Section titled “Solution 3”chmod +x makes the script executable.
chmod +x Module_1_Linux/fastq_summary.sh./fastq_summary.shSolution 4
Section titled “Solution 4”mkdir -p creates the folder, and > redirects output to a file.
mkdir -p Module_1_Linux/resultsModule_1_Linux/fastq_summary.sh > Module_1_Linux/results/long_reads_summary.txtSolution 5
Section titled “Solution 5”Use wc -l for lines and divide by 4 for reads.
#!/usr/bin/env bash
FILES=( "Training/long_reads/barcode57.fastq" "Training/long_reads/barcode58.fastq" "Training/long_reads/sample3.fastq" "Training/short_reads/paired/SRR1553607_1.fastq" "Training/short_reads/paired/SRR1553607_2.fastq")
for f in "${FILES[@]}"; do lines=$(wc -l < "$f") reads=$((lines / 4)) echo "FILE: $f" echo "READS: $reads" head -n 1 "$f" echo ""done