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 blockWhy it matters
variablesstore values for reuse
argumentslet scripts accept outside input
conditionsdecide what should happen next
loopsrepeat work safely
outputshow useful information
exit statussignal success or failure

Bash examples

PatternExample
variablename=\"value\"
argument$1
conditionif [ -f file.txt ]; then ... fi
loopfor item in a b c; do ... done

PowerShell examples

PatternExample
variable$name = \"value\"
argumentparam([string]$Name)
conditionif (Test-Path file.txt) { ... }
loopforeach ($item in $items) { ... }

Python examples

PatternExample
variablename = \"value\"
argumentsys.argv or argparse
conditionif os.path.exists(\"file.txt\"):
loopfor 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