Control Flow
Back to Week 3
This page continues the Bash scripting tutorial by introducing conditionals, loops, and common control-flow patterns. These constructs allow scripts to make decisions, repeat tasks, and automate complex workflows.
In Bash, commands return an exit code:
0→ success (true)- non-zero → failure (false)
You can check the last exit code with:
This is the foundation for conditionals in shell scripts.Conditionals
Basic if/else statements
Test Expressions
| Test | Meaning |
|---|---|
-f file |
Regular file exists |
-d dir |
Directory exists |
-e path |
Path exists |
-r file |
Readable |
-w file |
Writable |
-x file |
Executable |
String Tests
| Test | Meaning |
|---|---|
-z str |
String is empty |
-n str |
String is not empty |
str1 == str2 |
Equal |
str1 != str2 |
Not equal |
Numeric Comparisons
| Operator | Meaning |
|---|---|
-eq |
equal |
-ne |
not equal |
-lt |
less than |
-le |
less or equal |
-gt |
greater than |
-ge |
greater or equal |
Logical Operators
| Operator | Meaning |
|---|---|
&& |
AND |
|| |
OR |
! |
NOT |
Loops
Basic For loop
Loop Over Files
Numeric Loop
Alternatively,
while Loops
While loops are also available in bash. Note that this example uses an expression to increment count.
Reading files with while
One of the most common uses of a while loop is using a file. Note that this example uses a < to read input from a file. We'll talk more about this in the pipes section!
Examples
#!/bin/bash
if [[ -z "$SLURM_JOB_ID" ]]; then
echo "Must be run inside Slurm"
exit 1
fi
for gpu in $(nvidia-smi --query-gpu=index --format=csv,noheader); do
echo "GPU $gpu active"
done
for input in $(ls -d ./replicate*); do
sbatch myjob.sub $input
sleep 0.1
done
Common Pitfalls
Warning
- Spaces matter:
[ "$x" = 1 ], not["$x"=1] - Use quotes to avoid word splitting
- Remember:
0means success - If a command fails, bash will continue to move on through the script!
Next Section: Pipes