Script Building Blocks
Summary
This note is a quick reference for the ideas and syntax patterns that appear again and again in small scripts, regardless of the exact language.
When to use this note
- when you need a quick reminder of common scripting patterns
- when you remember the idea but not the syntax shape
- when you want to compare the same building block across Bash, PowerShell, and Python
Common building blocks
| Building block | Why it matters |
|---|
| variables | store values for reuse |
| arguments | let scripts accept outside input |
| conditions | decide what should happen next |
| loops | repeat work safely |
| output | show useful information |
| exit status | signal success or failure |
Bash examples
| Pattern | Example |
|---|
| variable | name=\"value\" |
| argument | $1 |
| condition | if [ -f file.txt ]; then ... fi |
| loop | for item in a b c; do ... done |
PowerShell examples
| Pattern | Example |
|---|
| variable | $name = \"value\" |
| argument | param([string]$Name) |
| condition | if (Test-Path file.txt) { ... } |
| loop | foreach ($item in $items) { ... } |
Python examples
| Pattern | Example |
|---|
| variable | name = \"value\" |
| argument | sys.argv or argparse |
| condition | if os.path.exists(\"file.txt\"): |
| loop | for item in items: |
Small code examples
Bash
name="Damian"
if [ -f file.txt ]; then
echo "$name found file.txt"
fi
PowerShell
param([string]$Name = "Damian")
if (Test-Path file.txt) {
Write-Host "$Name found file.txt"
}
Python
import os
name = "Damian"
if os.path.exists("file.txt"):
print(f"{name} found file.txt")
Notes
- the exact syntax changes by language, but the underlying ideas stay similar
- good scripting usually depends more on clear logic than on clever syntax
- this note should stay short and act as a memory aid, not a full tutorial
Common mistakes
- copying syntax from one language into another without adjusting the model
- focusing on syntax before deciding input, output, and failure handling
- treating a reference note as a substitute for testing the script safely