Scripting & Automation — Google SDE Interview Prep

Part 1: Questions 1–75


Section 1: Bash Fundamentals (Q1–Q20)


Q1: How do you declare and use variables in Bash? What are the quoting rules?

Answer: Variables are assigned without spaces around = and referenced with $. Single quotes preserve everything literally; double quotes allow variable expansion and command substitution; backslash escapes single characters.

Why this approach: Double-quoting all variable expansions prevents word-splitting and pathname expansion bugs that are a top source of shell script errors.


Q2: What is the difference between $@ and $*, and when does it matter?

Answer: Both hold positional parameters, but differ when quoted. "$@" expands each argument as a separate quoted word; "$*" joins all arguments into one word separated by IFS.

Why this approach: Always use "$@" when forwarding arguments to preserve each argument's boundaries exactly.


Q3: How do you declare and iterate over arrays in Bash?

Answer: Arrays use parentheses for declaration, ${arr[i]} for indexing, and ${arr[@]} to expand all elements.

Why this approach: Quoting "${arr[@]}" is essential — without quotes, elements with spaces break into separate words.


Q4: Explain for, while, and until loops with examples.

Answer: for iterates over a list or C-style range; while runs while a condition is true; until runs until a condition becomes true.

Why this approach: while IFS= read -r line is the canonical safe pattern for line-by-line file processing — IFS= preserves leading/trailing whitespace and -r prevents backslash interpretation.


Q5: What is the difference between [, [[, and (( ))?

Answer: [ is POSIX test; [[ is Bash's extended keyword with safer string handling; (( )) is arithmetic evaluation.

Why this approach: Prefer [[ ]] in Bash scripts for its safety features; use (( )) for all arithmetic to avoid the $ noise and get natural math semantics.


Q6: How do you write and call functions in Bash? How do you return values?

Answer: Functions are declared with name() { } or function name { }. They return an exit status (0–255) via return; to return data, use stdout capture or a nameref variable.

Why this approach: Using local for all function variables prevents accidentally clobbering globals; stdout capture is the cleanest way to return string/numeric data.


Q7: What are exit codes and how do you use $??

Answer: Every command exits with a code: 0 = success, 1–255 = error. $? holds the exit code of the most recently executed foreground command.

Why this approach: Always capture $? immediately after the command you care about — subsequent commands overwrite it.


Q8: What are subshells and how do they affect variable scope?

Answer: A subshell is a child process copy of the shell. Variable changes inside a subshell do not propagate back to the parent.

Why this approach: The pipe-subshell gotcha breaks counters and variable accumulation — process substitution < <(...) is the clean fix.


Q9: How do redirections work? Explain >, >>, 2>&1, and /dev/null.

Answer: Redirections rewire file descriptors. FD 0=stdin, 1=stdout, 2=stderr.

Why this approach: The ordering rule > file 2>&1 trips up many candidates — 2>&1 means "point FD2 to wherever FD1 currently points," so FD1 must be redirected first.


Q10: What does set -euo pipefail do and when should you use it?

Answer: These are safety options for production scripts: -e exits on error, -u errors on undefined variables, -o pipefail makes pipelines fail if any command fails.

Why this approach: set -euo pipefail converts silent failures (the default) into loud, obvious script termination — essential for any script run in production or CI/CD.


Q11: What are special variables $$, $!, $#, and $0?

Answer: Bash provides several automatic variables that expose runtime information about the script and its execution.

Why this approach: Using $$ in temp file names prevents collisions when multiple script instances run concurrently.


Q12: How does getopts work for parsing command-line options?

Answer: getopts is the POSIX-standard built-in for parsing short options (-f, -v, -o val). It handles option clustering and argument association automatically.

Why this approach: getopts is built-in (no external dependency), handles -vf data.txt clusters correctly, and the leading : in the option string enables silent error handling.


Q13: How do you write a heredoc and when is it useful?

Answer: A heredoc (<<DELIMITER) feeds a multiline string as stdin, preserving newlines. <<- strips leading tabs; quoting the delimiter prevents variable expansion.

Why this approach: Heredocs keep multiline content readable inline without escaping every quote, and quoting the delimiter ('EOF') is the safest way to pass literal text to commands like ssh or psql.


Q14: What is process substitution and how does it differ from command substitution?

Answer: Process substitution <(cmd) provides the output of a command as a named pipe (filename), allowing commands that require file arguments to consume command output. Command substitution $(cmd) captures output as a string.

Why this approach: Process substitution avoids temp files and — crucially — avoids the subshell variable-scope issue that plagues pipelines.


Q15: How do you handle signals with trap?

Answer: trap registers a handler that runs when the shell receives a signal or a pseudo-signal like EXIT, ERR, or DEBUG.

Why this approach: Registering cleanup on EXIT (rather than individual signals) is the most robust pattern — it fires on normal exit, errors, and caught signals alike.


Q16: Explain background jobs, &, wait, and job control.

Answer: Appending & runs a command in the background. wait blocks until background jobs finish. jobs, fg, bg, kill manage job control.

Why this approach: Capturing each PID immediately after & (before the next &) and storing in an array is the correct pattern for parallelism with proper error tracking.


Q17: How do mktemp and file locking with flock work?

Answer: mktemp creates uniquely named temporary files/directories safely. flock applies advisory locks so concurrent script instances don't race.

Why this approach: mv for atomic writes and flock for concurrency control are the two primitives that make scripts safe to run in parallel or via cron without races.


Q18: How do you write an atomic file update in Bash?

Answer: Write to a temp file on the same filesystem, then mv (rename), which is atomic at the kernel level — readers never see a partial file.

Why this approach: Writing to a temp file on the same filesystem ensures mv is a single rename(2) syscall — atomic by POSIX — so no process ever reads a half-written config.


Q19: How do you parse options with both short and long flags?

Answer: getopts only handles short options. For long options, use getopt (external), or a manual while/case loop.

Why this approach: Manual case parsing is portable, readable, and handles --flag=value and --flag value syntaxes without depending on GNU getopt.


Q20: How do you use set -euo pipefail together with error handling patterns?

Answer: Combining strict mode with trap ERR, conditional ||, and local set +e blocks lets you have both safety and controlled error handling.

Why this approach: The combination of set -euo pipefail + trap ERR + targeted || true/set +e gives you both fail-fast safety and the flexibility to handle expected non-zero exits gracefully.


Section 2: Text Processing (Q21–Q40)


Q21: What are the key grep flags and when do you use them?

Answer: grep searches for patterns; its flags control matching behavior, output format, and recursion.

Why this approach: -q with an if statement is the cleanest way to use grep as a boolean test without capturing output.


Q22: How does sed work for substitution, deletion, and in-place editing?

Answer: sed is a stream editor that applies commands to each line. s/// substitutes, d deletes, -i edits in-place.

Why this approach: -i.bak is safer than plain -i in production — you always have a rollback copy before testing a new sed expression.


Q23: How does awk work? Explain fields, NR, NF, patterns, BEGIN/END.

Answer: awk processes records (lines) splitting them into fields. NR=record number, NF=number of fields, $0=whole line, $1..$NF=fields.

Why this approach: awk associative arrays are the go-to for frequency counting and grouping tasks that would require multi-step pipelines in pure shell.


Q24: How do you use cut, sort, uniq together?

Answer: cut extracts columns, sort orders lines, uniq deduplicates (adjacent) lines. They compose naturally in pipelines.

Why this approach: The sort | uniq -c | sort -rn | head pipeline is a universal frequency analysis idiom every SRE should know by heart.


Q25: How does tr work and what are its common uses?

Answer: tr translates or deletes characters from stdin. It operates on individual characters (not strings/regex like sed).

Why this approach: tr -d '\r' is the fastest way to fix Windows line endings in a pipeline without touching the file, and tr -dc '0-9' is the simplest digit-extraction one-liner.


Q26: How does wc work and what are its flags?

Answer: wc counts lines, words, and bytes/characters in files or stdin.

Why this approach: wc -l < file (with input redirection) avoids the leading-space issue in $(wc -l file) output — the filename is omitted and the number is clean.


Q27: How does xargs work and when should you use it over a loop?

Answer: xargs builds and executes commands from stdin, batching arguments efficiently. It's faster than a shell loop for large input sets.

Why this approach: -print0 | xargs -0 is the only safe combination for filenames containing spaces, newlines, or special characters.


Q28: How does find -exec work and how does it compare to find | xargs?

Answer: find -exec cmd {} \; runs one command per found file; -exec cmd {} + batches multiple files per invocation (like xargs); | xargs offers more flexibility.

Why this approach: -exec {} + is preferred over \; for performance — it batches files into single command invocations exactly like xargs, but without the pipe.


Q29: How do you use paste to combine files and columns?

Answer: paste merges files line by line (horizontally), placing fields side by side with a delimiter.

Why this approach: paste -sd, is the fastest one-liner to join a file's lines with a custom delimiter — cleaner than an awk loop for simple serialization.


Q30: Write a one-liner to find the 5 most common words in a file.

Answer: Combine tr, sort, and uniq -c in a pipeline to tokenize, count, and rank.

Why this approach: The tr | sort | uniq -c | sort -rn | head pipeline is a universal pattern that works on any Unix system without scripting, but the awk single-pass version is more efficient for large files.


Q31: How do you extract specific fields from structured log files using awk?

Answer: Use awk with field separators matching the log format to extract, filter, and aggregate data.

Why this approach: awk with associative arrays handles the most common log aggregation tasks in a single pass without loading the entire file into memory.


Q32: How do you do multiline pattern matching with sed or awk?

Answer: sed can use address ranges (/start/,/end/); awk accumulates lines with a flag variable for multiline blocks.

Why this approach: awk's RS="" paragraph mode is underused but extremely powerful — it treats blank-line-separated blocks as single records, making multiline field extraction trivial.


Q33: How do you use sed for in-place configuration file editing in scripts?

Answer: Use sed -i with anchored patterns to safely modify specific config keys without disrupting surrounding content.

Why this approach: Using capture groups in sed substitutions (\1) is safer than rewriting the full line, because it preserves surrounding formatting and reduces the regex surface area.


Q34: How do you remove duplicates from a file while preserving order?

Answer: sort -u loses order. To preserve insertion order, use awk with an associative array as a seen-set.

Why this approach: awk '!seen[$0]++' is a canonical one-liner — the ! test and ++ post-increment combine into a single elegant gate that every senior engineer should recognize.


Q35: How do you perform arithmetic in Bash and handle floating-point?

Answer: Bash only handles integers natively via $(( )) or let. For floating-point, use bc, awk, or Python.

Why this approach: Using awk 'BEGIN { exit !(expr) }' is the cleanest way to use a floating-point comparison as a shell conditional without spawning a Python process.


Q36: How do you extract and transform JSON with command-line tools?

Answer: jq is the standard tool for JSON processing; python3 -m json.tool validates; awk/grep work for simple cases.

Why this approach: jq -r (raw output) is essential when using jq results in shell variables — without it, string values are double-quoted and break downstream commands.


Q37: How do you process CSV files in Bash?

Answer: For simple CSVs use cut/awk; for files with quoted fields or embedded commas, use python3 or mlr (Miller).

Why this approach: For production scripts with CSVs that might contain quoted fields, always use Python's csv module — awk/cut fail silently on embedded commas and quotes.


Q38: How do you use tee for logging and splitting output?

Answer: tee writes stdin to both a file and stdout simultaneously, enabling output logging without breaking pipelines.

Why this approach: exec > >(tee -a logfile) 2>&1 at the top of a script is a clean way to make the entire script's output self-logging without wrapping every command.


Q39: How do you use grep with regular expressions for log analysis?

Answer: Combine grep's regex modes with context flags and output formatting for efficient log triage.

Why this approach: --line-buffered on grep in a tail -f pipeline is required to see results in real time — without it, grep buffers output and you get delayed or no output.


Q40: How do you write a complete log-parsing pipeline to summarize an Apache access log?

Answer: Chain awk, sort, uniq, and head to extract the key metrics an operator needs at a glance.

Why this approach: Breaking each metric into its own pipeline stage makes the script easy to extend, debug, and reuse — each section is independently testable.


Section 3: File & Process Automation (Q41–Q55)


Q41: How do you safely copy, move, and remove files in scripts?

Answer: Use flags that prevent accidental overwrites, verbose output for logging, and always quote paths.

Why this approach: Anchoring rm -rf with a path prefix guard ($target == /tmp/*) is a critical safety check — a variable expansion to empty string would otherwise rm -rf /.


Q42: How do you use find for file cleanup tasks?

Answer: find with -mtime, -size, -name, and -exec builds powerful cleanup pipelines.

Why this approach: -delete is safer than -exec rm {} + because find won't delete a directory unless it's empty, and it's faster since no shell process is spawned.


Q43: How do you monitor file system changes with inotifywait?

Answer: inotifywait (from inotify-tools) blocks until a filesystem event occurs — ideal for trigger-based automation.

Why this approach: Piping inotifywait -m output into a while read loop is the standard pattern for event-driven file processing — it's efficient because it uses kernel inotify events rather than polling.


Q44: How do you find and manage processes with lsof and fuser?

Answer: lsof (list open files) and fuser identify which processes are using files, sockets, or devices.

Why this approach: lsof | grep deleted is an essential SRE technique — when a log file is deleted but a process still writes to it, disk space isn't freed until the process closes the FD.


Q45: How do you write a process monitoring script?

Answer: Combine ps, pgrep, kill, and a loop to watch and restart processes.

Why this approach: pgrep -x matches the exact process name (not substring), preventing false positives when multiple services share a name prefix.


Q46: How do you implement a retry loop in Bash?

Answer: Wrap the command in a for or while loop with exponential backoff and a max-retry limit.

Why this approach: Exponential backoff is essential for retrying distributed system calls — linear retries can amplify thundering-herd problems during outages.


Q47: How do you use nohup and disown to keep processes running after logout?

Answer: nohup makes a command immune to SIGHUP (sent on terminal close); disown removes a job from the shell's job table after it's started.

Why this approach: Redirecting stdin from /dev/null in nohup commands prevents the background process from blocking on terminal input, which would hang it silently.


Q48: How do you write a script that handles cleanup on unexpected termination?

Answer: Use trap EXIT ERR INT TERM with a comprehensive cleanup function to ensure resources are released regardless of how the script exits.

Why this approach: Registering cleanup on EXIT (not just INT/TERM) ensures it runs even on exit calls from set -e, making the cleanup unconditional.


Q49: How do you do parallel processing in shell scripts?

Answer: Background jobs with & + wait, xargs -P, or GNU parallel provide different levels of parallelism control.

Why this approach: xargs -P is the simplest parallel approach for embarrassingly parallel file operations; use sem from GNU parallel when you need a semaphore without rewriting the whole pipeline.


Q50: How do you write a cron-safe script with logging and locking?

Answer: A cron script needs: exclusive locking, self-logging, sane environment, and clean exit codes.

Why this approach: Exiting 0 when the lock is already held is the correct cron behavior — a non-zero exit would generate a cron email alert for every normal overlap, causing alert fatigue.


Q51: How do you send alerts from a script (email, Slack, PagerDuty)?

Answer: Scripts can send alerts via mail/sendmail, curl to webhook APIs, or dedicated CLI tools.

Why this approach: Using curl with a Slack/PagerDuty webhook requires no additional packages and works identically across environments, making it the most portable alerting mechanism.


Q52: How do you write a script to rotate and archive log files?

Answer: Implement rotation by renaming, compressing, and pruning old files — or use logrotate configuration for production.

Why this approach: Truncating with > "$log" (rather than rm + recreate) is safe for processes with open file descriptors — they continue writing to the same inode without error.


Q53: How do you watch a directory and trigger actions on new files using a polling approach?

Answer: When inotifywait is unavailable, poll with find and a timestamp marker file, or compare directory snapshots.

Why this approach: Using a marker file with -newer is simpler and more reliable than snapshot-diffing — touch is atomic and the -newer test leverages the kernel's mtime tracking.


Q54: How do you implement a health check script for a service?

Answer: A health check tests multiple dimensions: process running, port open, HTTP response, and response time.

Why this approach: Accumulating failures with EXIT_CODE=1 rather than exiting on the first failure gives a complete health picture in one script run.


Q55: How do you write a deployment script with rollback?

Answer: A deploy script captures current state before changes, applies changes atomically, verifies success, and rolls back automatically on failure.

Why this approach: ln -sfn for the current symlink is atomic — the old release stays live until the instant the symlink swaps, giving zero-downtime cutover.


Section 4: Python for SysAdmin — Basics (Q56–Q75)


Q56: How do you use os and os.path for filesystem operations?

Answer: os provides POSIX-level syscall wrappers; os.path handles path string manipulation portably.

Why this approach: os.path.join should always be used instead of string concatenation — it handles trailing slashes and platform separators correctly.


Q57: How does shutil simplify high-level file operations?

Answer: shutil provides copy, move, delete, and archive operations that os lacks or makes cumbersome.

Why this approach: shutil.copytree with ignore_patterns is the cleanest way to copy a project directory while excluding build artifacts — far simpler than equivalent os.walk logic.


Q58: How does subprocess.run work and when do you use check=True and capture_output?

Answer: subprocess.run is the modern high-level API for running external commands. check=True raises CalledProcessError on non-zero exit; capture_output=True captures stdout and stderr.

Why this approach: Always prefer the list form ['cmd', 'arg'] over shell=True — the list form avoids shell injection vulnerabilities and handles arguments with spaces correctly.


Q59: When do you use subprocess.Popen instead of subprocess.run?

Answer: Popen is the low-level API for streaming I/O, bidirectional communication, or when you need the process handle before it finishes.

Why this approach: proc.stdout.close() after setting p2.stdin=p1.stdout is essential — without it, p1 won't receive SIGPIPE when p2 exits, and the pipeline hangs.


Q60: How does pathlib improve on os.path?

Answer: pathlib.Path is an object-oriented path API that's more readable, composable, and Pythonic than os.path string functions.

Why this approach: The / operator for path joining makes path construction read like a path rather than a nested function call — base / 'subdir' / 'file.txt' is instantly clear.


Q61: How do you write a robust argument parser with argparse?

Answer: argparse provides type checking, help generation, subcommands, and mutual exclusion groups.

Why this approach: parser.error() prints the usage and error message then exits with code 2, matching the POSIX convention for argument errors — don't use sys.exit() directly for argument validation.


Q62: How do you read and write files safely in Python?

Answer: Always use with statements for automatic close; use explicit encoding; handle exceptions; use pathlib or tempfile for atomic writes.

Why this approach: os.replace() (Python 3.3+) is atomic on POSIX — it wraps rename(2) — so readers never observe a partial write, even if the process crashes mid-way.


Q63: How do you handle exceptions properly in Python sysadmin scripts?

Answer: Catch specific exceptions, log the full traceback for debugging, use finally for cleanup, and distinguish expected from unexpected errors.

Why this approach: Catching Exception at the top level while letting SystemExit and KeyboardInterrupt propagate (they inherit from BaseException, not Exception) is the correct Python exception hierarchy usage.


Q64: How do you configure the logging module for production scripts?

Answer: Use logging.basicConfig for simple scripts; dictConfig or fileConfig for production; add handlers for both file and stderr.

Why this approach: Setting the root logger to DEBUG and filtering at the handler level gives you full flexibility — you can enable verbose file logging while keeping the console clean without restarting.


Q65: How do you work with environment variables in Python?

Answer: Use os.environ for reading/writing; os.environ.get() for defaults; os.environ.setdefault() and python-dotenv for config loading.

Why this approach: os.environ.get('KEY', default) is preferred over os.environ['KEY'] in scripts because a missing variable raises a KeyError with no context — the get + explicit error message pattern gives actionable output.


Q66: How do you read and write JSON in Python?

Answer: Use json.load/json.dump for files and json.loads/json.dumps for strings. Handle JSONDecodeError for invalid input.

Why this approach: Adding f.write('\n') after json.dump is a small but important detail for POSIX compliance — many tools (wc -l, diff, git) behave correctly only on newline-terminated files.


Q67: How do you read and write CSV files in Python?

Answer: Use the csv module for proper quoting and encoding; use DictReader/DictWriter for header-based access.

Why this approach: Always open CSV files with newline='' as recommended by the Python docs — the csv module handles its own universal newline translation, and omitting newline='' causes double-CR issues on Windows.


Q68: How do you use os.walk and pathlib.rglob for directory traversal?

Answer: os.walk generates (dirpath, dirnames, filenames) tuples top-down; pathlib.rglob is cleaner for pattern matching.

Why this approach: Modifying dirnames in-place (dirnames[:] = [...]) is the correct way to prune os.walk — it prevents recursion into excluded directories without spawning a new walk.


Q69: How do you run multiple subprocesses in parallel with Python?

Answer: Use concurrent.futures.ThreadPoolExecutor for I/O-bound subprocess tasks or a list of Popen objects for fine-grained control.

Why this approach: ThreadPoolExecutor with subprocess.run is the cleanest parallel subprocess pattern — threads release the GIL while waiting for subprocesses, so there's no CPU contention despite using threads.


Q70: How do you use Python's tempfile module safely?

Answer: tempfile creates uniquely named, securely opened temporary files and directories, avoiding race conditions in /tmp.

Why this approach: tempfile.NamedTemporaryFile(delete=False) + explicit os.replace + cleanup in except is the safest atomic write pattern — tempfile guarantees no name collision and secure file creation (mode 0600).


Q71: How do you parse and validate configuration files in Python?

Answer: Use configparser for INI files, json for JSON, tomllib (3.11+) for TOML, and a validation layer with clear error messages.

Why this approach: Validating config at startup with clear error messages (rather than letting KeyError crash mid-execution) is a sign of production-quality tooling — operators need to know what to fix before the service starts.


Q72: How do you implement a simple HTTP client for API calls in Python?

Answer: Use urllib.request for no-dependency scripts; requests or httpx for production code.

Why this approach: Mounting a Retry adapter on a requests.Session is the correct way to add retry logic — it handles connection errors and server errors transparently without wrapping every call in a try/except loop.


Q73: How do you use Python's hashlib and hmac for file integrity and authentication?

Answer: hashlib computes digests for file integrity; hmac adds a secret key for authentication (MAC).

Why this approach: hmac.compare_digest performs a constant-time comparison — using == instead is vulnerable to timing attacks that can reveal the secret key one byte at a time.


Q74: How do you write a Python script that tails and parses a log file in real time?

Answer: Open the file, seek to end, and poll for new content — mirroring tail -f behavior.

Why this approach: f.seek(0, 2) (seek to end before starting) ensures you only process new lines and don't replay the entire history — essential for long-running log files.


Q75: How do you structure a complete Python sysadmin script with all best practices?

Answer: Combine argparse, logging, config loading, error handling, subprocess, and clean exit into a production-ready template.

Why this approach: This template demonstrates all the patterns Google interviewers look for: structured logging, validated config, argparse with sensible defaults, parallel execution, clean exit codes, and a __name__ == '__main__' guard that makes the module importable and testable.

Part 2A: Questions 76–112


Section 1: Python Advanced SysAdmin (Q76–Q95)


Q76: How do you run a shell command from Python and capture its output using subprocess with pipes?

Answer: Use subprocess.run() or subprocess.Popen() with stdout=subprocess.PIPE and stderr=subprocess.PIPE. For simple cases, subprocess.run() with capture_output=True is the cleanest approach. For streaming or interactive processes, use Popen with communicate().

Why this approach: communicate() reads all output at once and closes the streams, preventing deadlocks caused by full pipe buffers.


Q77: How do you use paramiko to SSH into a remote host, run a command, and retrieve output?

Answer: Paramiko provides a Python SSH client. Use SSHClient.connect() to authenticate, exec_command() to run commands, and read from the returned file-like objects for stdout/stderr. For SFTP file transfers, use open_sftp().

Why this approach: recv_exit_status() blocks until the remote process finishes, ensuring you capture complete output rather than reading prematurely.


Q78: How do you use the Python requests library to make GET and POST calls with headers, authentication, and timeouts?

Answer: requests abstracts HTTP connections cleanly. Pass headers=, auth=, and timeout= as keyword arguments. Use a Session object to reuse connections and share headers/auth across multiple requests.

Why this approach: A Session with a mounted retry adapter handles transient network failures automatically without cluttering business logic with retry loops.


Q79: How do you parse and generate JSON in Python? Cover json.load, json.loads, and json.dumps.

Answer: Python's json module handles serialization and deserialization. load() reads from a file object, loads() parses a string, and dumps() serializes to a string (dump() writes to a file).

Why this approach: Using raise_for_status() before json.loads() on HTTP responses and wrapping json.load() in try/except prevents silent data corruption from malformed inputs.


Q80: How do you safely parse YAML configuration files in Python using PyYAML?

Answer: Use yaml.safe_load() instead of yaml.load() to avoid arbitrary code execution via YAML tags. For writing, use yaml.dump(). Install with pip install pyyaml.

Why this approach: safe_load() only handles standard YAML types, preventing !!python/object tags from executing arbitrary Python code on untrusted config files.


Q81: How do you read and write CSV files in Python, including handling headers and custom delimiters?

Answer: Python's csv module handles quoting, escaping, and newlines correctly. Use DictReader and DictWriter for named-column access, which is more readable and robust than index-based access.

Why this approach: DictReader/DictWriter decouple code from column order, making scripts resilient to column additions or reordering in the CSV schema.


Q82: How do you use concurrent.futures.ThreadPoolExecutor to parallelize I/O-bound tasks in Python?

Answer: ThreadPoolExecutor manages a pool of threads for concurrent I/O work (network calls, disk reads). Use submit() for individual futures or map() for uniform task lists. Always handle exceptions from futures explicitly.

Why this approach: as_completed() processes results as they finish rather than waiting for the slowest task, improving perceived responsiveness in monitoring scripts.


Q83: What are the basics of Python multiprocessing and when should you use it over threading?

Answer: Python's GIL prevents true parallelism in threads for CPU-bound work. multiprocessing spawns separate OS processes, each with its own GIL and memory space, enabling true parallelism for CPU-intensive tasks like compression, hashing, or data processing.

Why this approach: Pool.map() automatically partitions the work list and collects results in order, handling process lifecycle and IPC transparently.


Q84: How do you use tempfile.NamedTemporaryFile safely for temporary files in scripts?

Answer: tempfile.NamedTemporaryFile creates a secure temporary file with a unique name in the system's temp directory. Use it as a context manager so it is automatically deleted on exit, even if an exception occurs.

Why this approach: os.replace() on the same filesystem is atomic, so readers never see a partially written file even if the writer crashes mid-write.


Q85: How do you use glob and fnmatch to find files matching patterns in Python?

Answer: glob.glob() and Path.glob() expand shell-style wildcards to file paths. fnmatch tests a single filename against a pattern without filesystem access, useful for filtering lists already in memory.

Why this approach: Path.rglob() from pathlib returns Path objects directly, enabling method chaining like .stat(), .read_text(), and .unlink() without extra os.path calls.


Q86: How do you use Python's re module for regex matching, searching, extracting groups, and substitution?

Answer: Compile patterns with re.compile() for reuse. Use search() for anywhere-in-string matches, match() for start-of-string, findall() for all occurrences, and sub() for replacement.

Why this approach: Named groups ((?P<name>...)) make regex patterns self-documenting and allow accessing captures by semantic name rather than fragile positional indices.


Q87: How do you use hashlib to compute MD5 and SHA-256 checksums of files and strings in Python?

Answer: hashlib provides a uniform interface for all hash algorithms. Always read files in chunks to handle large files without exhausting memory. Use SHA-256 for security-sensitive contexts; MD5 only for non-security checksums (e.g., cache keys, deduplication).

Why this approach: hmac.compare_digest() uses a constant-time comparison that prevents timing attacks when verifying checksums in security-sensitive contexts.


Q88: What makes a script idempotent and how do you write idempotent deployment or configuration scripts?

Answer: An idempotent script produces the same result regardless of how many times it is run. The key principle is "check before act": test whether the desired state already exists before taking action. This is critical for safe re-runs after partial failures.

Why this approach: Checking current state before acting makes scripts safe to run in CI/CD pipelines and disaster recovery scenarios where partial failures require re-running without manual cleanup.


Q89: How should you use sys.exit() with meaningful exit codes in production scripts?

Answer: Scripts should communicate success, failure, and specific error conditions through exit codes. sys.exit(0) means success; non-zero indicates failure. Define named constants for codes to make scripts self-documenting and compatible with monitoring systems.

Why this approach: Defined exit code constants make the script's contract explicit and allow shell scripts, Nagios, and CI systems to branch on specific failure modes rather than treating all non-zero exits identically.


Q90: How do you use configparser to read and write INI-style configuration files in Python?

Answer: configparser.ConfigParser handles INI files with [sections] and key = value pairs. It supports type coercion, default values, interpolation, and fallback values — making it a robust alternative to ad-hoc config parsing.

Why this approach: The fallback= parameter in get() eliminates KeyError exceptions for optional config keys while keeping the "required vs optional" semantics visible at the call site.


Q91: How do you safely handle secrets and credentials in Python scripts — avoiding hardcoded values?

Answer: Credentials should come from environment variables, secret management systems (Vault, AWS Secrets Manager), or encrypted files — never hardcoded in source. Use os.environ for env vars and the relevant SDK for secret stores.

Why this approach: Centralizing credential loading into a Config class with _require() fails fast at startup with a clear error, rather than failing deep in business logic with a confusing NoneType error.


Q92: How do you write a Python script that logs to both a file and stdout with different log levels?

Answer: Configure the logging module with multiple handlers — a StreamHandler for stdout and a RotatingFileHandler for the log file. Set different levels on each handler to control verbosity per destination.

Why this approach: Separating log levels per handler lets operators get clean INFO summaries on the console while preserving full DEBUG traces in the file for post-incident analysis.


Q93: How do you handle argument parsing in Python scripts using argparse for production-quality CLIs?

Answer: argparse generates help text, type validation, and usage errors automatically. Use subcommands for scripts with multiple operations, type= for coercion, and required= groups for mutually exclusive options.

Why this approach: Subcommands keep related argument groups isolated and allow each subcommand to have its own --help, making complex scripts navigable without reading source code.


Q94: How do you write a Python context manager for resource cleanup and atomic operations?

Answer: Implement __enter__ / __exit__ for class-based context managers, or use contextlib.contextmanager with a generator. Context managers guarantee cleanup even when exceptions occur.

Why this approach: contextlib.ExitStack lets you dynamically register cleanup callbacks and open a variable number of resources under one with block, eliminating nested with pyramids.


Q95: How do you use Python's shutil module for file system operations like copying, moving, and archiving?

Answer: shutil provides high-level file and directory operations that handle edge cases (permissions, symbolic links, metadata) better than manual open()/write() approaches. Use it for deployment scripts that manipulate directory trees.

Why this approach: shutil.make_archive() and unpack_archive() handle format detection and cross-platform differences automatically, avoiding subprocess calls to tar or zip that differ between Linux and macOS.


Section 2: Cron & Scheduling (Q96–Q105)


Q96: Explain all five fields of a crontab entry with concrete examples.

Answer: A crontab line has the format: minute hour day-of-month month day-of-week command. Each field accepts numbers, ranges (1-5), lists (1,3,5), steps (*/15), or * (any). The command runs whenever all five fields match the current time.

Why this approach: Breaking the format into visual columns with a comment header prevents field transposition errors — the most common crontab mistake.


Q97: What are the @reboot, @daily, @hourly, and other cron shortcut expressions?

Answer: Cron shortcut strings replace the five-field syntax for common intervals. @reboot is particularly useful for starting services or scripts when the system boots up.

Why this approach: Shortcut strings are self-documenting and immune to typos in field positioning, reducing the chance of accidentally scheduling a daily job to run every minute.


Q98: How do systemd timer units work, and how do they compare to cron?

Answer: A systemd timer unit (.timer) activates a paired service unit (.service) on a schedule defined by OnCalendar=. Timers support dependencies, logging to journald, and missed-run catch-up — advantages over cron for system services.

Cron vs. systemd timers comparison:

Why this approach: Persistent=true ensures a missed backup (due to maintenance downtime) runs on next boot, eliminating the silent data loss gap that cron silently ignores.


Q99: How do you use the at command for one-shot deferred job execution?

Answer: at schedules a command to run once at a specific future time, unlike cron which repeats. It reads commands from stdin and emails output to the user. Use atq to list pending jobs and atrm to cancel them.

Why this approach: at is simpler and safer than adding a one-time cron entry because it doesn't require remembering to remove the entry afterward, preventing accidental recurring execution.


Q100: How do you prevent overlapping cron job runs using flock?

Answer: flock acquires an exclusive file lock before running a command. If the lock is held (previous run still active), the new invocation exits immediately. This prevents resource contention from slow-running jobs that overlap.

Why this approach: File-based locking with flock is atomic at the kernel level and automatically released if the process crashes or is killed, unlike PID file approaches that require manual cleanup on unclean exits.


Q101: How do you redirect cron job stdout and stderr to a log file?

Answer: Cron captures stdout and emails it to the user (if MAILTO is set), which can flood inboxes. Redirect output to a log file in the crontab entry itself, and use 2>&1 to capture stderr alongside stdout.

Why this approach: Appending with >> preserves history across runs, while 2>&1 before the log path ensures stderr goes to the same file, making log parsing straightforward.


Q102: What PATH and environment variable gotchas should you know for cron jobs?

Answer: Cron runs with a minimal environment — typically PATH=/usr/bin:/bin and no user profile loaded. Scripts that work interactively often fail in cron because tools are in /usr/local/bin, environment variables are unset, or shell aliases are unavailable.

Why this approach: Using absolute paths in both the crontab entry and within called scripts eliminates PATH-dependent failures and makes the cron job behavior identical regardless of the invoking user's shell configuration.


Q103: How does MAILTO work in cron, and how do you configure email notifications?

Answer: Cron emails any stdout/stderr output to the user specified in MAILTO. Set MAILTO="" to disable emails entirely, or point it to an email address for alerting. Many production systems route cron mail through a local MTA to a monitoring system.

Why this approach: Healthchecks.io-style "ping on success" alerting inverts the model — you get alerted when the job doesn't run rather than when it does, catching failures caused by cron daemon crashes or misconfigured schedules.


Q104: How do you run a cron job as a specific user with crontab and /etc/cron.d/?

Answer: System-wide cron jobs in /etc/cron.d/ include a sixth field (the username) between the time fields and the command. This is the standard way to run scheduled jobs as non-root service accounts without modifying individual user crontabs.

Why this approach: Files in /etc/cron.d/ are version-controllable, can be deployed by configuration management (Ansible, Chef, Puppet), and specify the runtime user explicitly — all of which improve auditability over ad-hoc crontab -e edits.


Q105: How do you debug a cron job that works interactively but not when scheduled?

Answer: Cron failures are almost always environment-related (PATH, env vars) or permission-related. The debugging workflow is: capture the cron environment, replicate it in a shell, then test interactively. Check syslog/journald for cron daemon messages.

Why this approach: Capturing the actual cron environment to a file is the definitive diagnostic step because it reveals the exact PATH, locale, and variable differences that cause environment-dependent failures.


Section 3: Log Parsing & Monitoring (Q106–Q112)


Q106: How do you use tail -f and multitail to monitor live log files?

Answer: tail -f follows a file as new lines are appended, making it essential for watching live application logs. multitail extends this to watch multiple files simultaneously in a split terminal window with optional color highlighting and filtering.

Why this approach: tail -F (capital F) handles log rotation transparently by following the filename rather than the file descriptor, preventing the common mistake of watching a rotated-away old file after logrotate runs.


Q107: How do you parse Apache/nginx access logs with awk to extract useful metrics?

Answer: Access logs follow a well-known format. awk field separators and pattern matching extract status codes, request paths, response sizes, and client IPs efficiently in a single pass without loading the whole file into memory.

Why this approach: Single-pass awk processing avoids loading the entire log into memory, making it practical for multi-gigabyte access logs where python or perl approaches would require careful streaming implementation.


Q108: How do you count HTTP 5xx error rates per minute from an nginx access log?

Answer: Group log entries by the minute portion of the timestamp and count 5xx responses per minute using awk. This is useful for detecting error rate spikes in post-incident analysis or real-time monitoring pipelines.

Why this approach: Parsing the minute from the log timestamp with awk's split() avoids calling date for every line, keeping the per-line processing time O(1) and maintaining throughput on high-traffic logs.


Q109: How do you write a threshold-based alerting script that monitors error rates?

Answer: A monitoring script reads a metric, compares it to a threshold, and sends an alert (email, Slack, PagerDuty) if exceeded. Implement hysteresis (alert-on-trigger, resolve-on-clear) using a state file to avoid alert storms.

Why this approach: The state file implements hysteresis — once an alert fires, subsequent checks only re-alert if it resolves and re-triggers, preventing a noisy stream of identical alerts during a sustained incident.


Q110: How do you write a logrotate configuration file?

Answer: logrotate automates log file rotation, compression, and cleanup. Write a config file in /etc/logrotate.d/ for each application. Key directives: rotate, compress, dateext, postrotate, create, and notifempty.

Why this approach: sharedscripts with delaycompress is the correct combination for most web servers: the postrotate signal is sent once (not once per file), and the just-rotated file stays uncompressed for one cycle in case the server still has it open.


Q111: How do you write a cron script that monitors disk usage and emails when a threshold is exceeded?

Answer: Use df to check disk usage, compare against a threshold, and use mail or a curl webhook to notify. Track alert state in a file to implement hysteresis and prevent repeat emails.

Why this approach: Using per-partition state files with hysteresis prevents alert flooding when disk usage hovers near the threshold, and enables a clear resolved notification when the issue is remediated.


Q112: How do you combine awk, sort, and uniq to generate a full access log summary report?

Answer: Chain awk, sort, and uniq -c to aggregate log data into a readable report. A wrapper script that runs multiple awk passes produces a comprehensive summary useful for daily reporting or incident analysis.

Why this approach: Stripping query strings with sed before uniq -c aggregates /api/users?page=1 and /api/users?page=2 into a single /api/users entry, giving a clean view of endpoint popularity rather than fragmenting counts across parameter variations.

Part 2B: Questions 113–150


Log Parsing & Monitoring continued

Q113: How do you write a health-check script with proper exit codes (0=ok, 1=warn, 2=crit)?

Answer: Health-check scripts must communicate severity through exit codes so orchestration tools (nagios, k8s liveness probes, systemd) can take action automatically. Exit 0 = healthy, 1 = degraded but operational, 2 = critical failure.

Why this approach: Using a numeric accumulator for status guarantees the worst severity wins, and printing before exit lets monitoring systems capture both the human-readable message and the machine-readable code.


Q114: How do you monitor CPU, memory, and disk thresholds in Python?

Answer: psutil exposes cross-platform system metrics with a clean API; combining it with a threshold dictionary makes the logic easy to extend.

Why this approach: psutil.cpu_percent(interval=1) blocks for one second to get an accurate measurement rather than a cached snapshot, making the value meaningful for alerting.


Q115: How do you send an alert via curl to a Slack webhook?

Answer: Slack's Incoming Webhooks accept a JSON POST; formatting the payload correctly and checking the response prevents silent failures.

Why this approach: Capturing the HTTP status code with -w "%{http_code}" and -o /dev/null lets the script fail loudly if Slack rejects the payload without printing the raw HTML error page to stdout.


Q116: How do you watch a logfile for a pattern and send an alert?

Answer: tail -F follows rotations; a while read loop processes each new line in real time without polling overhead.

Why this approach: The cooldown gate prevents alert storms when an error floods the log, ensuring on-call engineers get one notification per minute rather than thousands.


Q117: How do you rotate logs manually in Bash?

Answer: Manual rotation timestamps the current log, compresses it, and truncates (not removes) the live file so running processes keep their open file descriptor.

Why this approach: Truncating with > "$LOGFILE" preserves the inode so any process writing to the file by file descriptor continues without error, unlike rm followed by touch which would cause writes to go nowhere.


Q118: How do you parse structured (JSON) logs with Python's json module?

Answer: JSON logs have one JSON object per line; reading line by line and calling json.loads handles multi-GB files without loading everything into memory.

Why this approach: Processing line by line keeps memory constant regardless of log size, and catching json.JSONDecodeError per line means a single malformed entry does not abort analysis of the entire file.


Q119: How do you extract latency percentiles from a log file?

Answer: Collect all matching numeric values into a list, sort it, then index by percentile position; statistics module handles the math cleanly.

Why this approach: Sorting once and indexing by fractional position is O(n log n) and handles any sample size; using statistics.mean avoids floating-point accumulation error from a hand-rolled sum.


Q120: How do you tail multiple log files simultaneously and label each line?

Answer: multitail or a simple Python threading approach lets you watch several files in parallel with source labels—critical for correlating events across services.

Why this approach: Daemon threads automatically die when the main thread exits, so KeyboardInterrupt cleanly terminates all watchers without needing explicit thread joins or stop events.


API & Network Automation

Q121: How do you use curl with Bearer and Basic auth headers?

Answer: Auth headers are passed with -H; Bearer tokens go in the Authorization header directly, while Basic auth can use -u (curl handles base64 encoding) or an explicit header.

Why this approach: Using -u user:pass keeps credentials out of the Authorization header construction in your script and lets curl handle encoding, but --netrc is best for production scripts where secrets must not appear in process arguments.


Q122: How do you POST JSON with curl?

Answer: Set Content-Type: application/json and pass the body with -d; use a heredoc or a file for complex payloads to avoid quoting nightmares.

Why this approach: Passing complex JSON as a file with -d @filename eliminates shell-escaping issues and keeps the script readable when the payload has multiple fields or nested objects.


Q123: How do you parse curl's HTTP response code separately from the response body?

Answer: --write-out with %{http_code} appends metadata after the body; redirect body to a temp file to separate the two.

Why this approach: Writing the body to a temp file and status to stdout via -w "%{http_code}" cleanly separates concerns so the caller can branch on the numeric code without trying to parse a JSON body that may contain the number 200.


Q124: How do you implement retry with exponential backoff in Bash?

Answer: A loop with sleep $((base ** attempt)) (or approximated with bit-shift) retries transient failures while not hammering a struggling service.

Why this approach: Jitter (random fraction of the backoff interval) prevents the thundering-herd problem where every retry client wakes up simultaneously after the same sleep duration and overwhelms the recovering service.


Q125: How do you retry with exponential backoff in Python using requests and tenacity?

Answer: tenacity decorators declaratively attach retry logic to any function, keeping business logic clean while handling jitter, wait strategies, and stop conditions.

Why this approach: wait_exponential_jitter combines exponential growth with random jitter in one parameter, and before_sleep_log logs each retry automatically so operators can see how many retries occurred without adding manual print statements.


Q126: What are the most useful jq one-liners for filtering, mapping, and selecting?

Answer: jq transforms JSON inline in pipelines; mastering a handful of primitives covers 90% of real-world API response processing.

Why this approach: Knowing select, INDEX, @tsv, and the // alternative operator transforms jq from a pretty-printer into a full ETL tool, eliminating the need for Python for many API-to-shell pipelines.


Q127: How do you handle paginated API calls in Python?

Answer: A generator that follows next links decouples pagination logic from the caller and works lazily so you can break early without fetching unnecessary pages.

Why this approach: Generators keep memory usage proportional to one page at a time rather than the entire dataset, and the three strategies cover the most common pagination conventions without requiring changes to caller code.


Q128: How do you write a simple webhook receiver in Python?

Answer: http.server.BaseHTTPRequestHandler is in the standard library and handles POST bodies, request headers, and response codes with no external dependencies.

Why this approach: hmac.compare_digest does constant-time comparison to prevent timing attacks on signature verification, which is essential when the webhook endpoint is publicly reachable.


Q129: How do you check SSL certificate expiry with openssl and Python?

Answer: openssl s_client retrieves the certificate; openssl x509 -enddate parses the expiry; Python's ssl module provides a pure-Python alternative that works in scripts without subprocess.

Why this approach: The Python version uses the standard ssl module so no subprocess is needed, and connecting with a real TLS handshake also validates the certificate chain, catching misconfigured intermediates that openssl with -noverify would miss.


Q130: How do you bulk-test HTTP endpoints and report failures?

Answer: Use Python with concurrent.futures.ThreadPoolExecutor to check many endpoints in parallel, collecting results into a structured report.

Why this approach: ThreadPoolExecutor with as_completed fills the worker pool immediately and collects results as they arrive, meaning the total runtime is roughly max_latency rather than sum_of_all_latencies.


Q131: How do you implement rate limiting in a script?

Answer: A token-bucket or leaky-bucket algorithm in Python enforces a maximum request rate without over-sleeping; for Bash, a simple counter with sleep is often enough.

Why this approach: A token bucket allows short bursts (up to capacity) while enforcing a long-term average rate, which matches how most APIs define their limits and avoids wasting time with a fixed-sleep approach during quiet periods.


Q132: How do you make authenticated API calls and cache responses to avoid redundant requests?

Answer: A simple file-based or in-memory cache with a TTL prevents hammering an API for data that rarely changes, which is important when scripts run frequently.

Why this approach: File-based caching survives across script invocations and processes (unlike in-memory dicts), making it useful for cron jobs that share state without a running daemon.


Q133: How do you upload a file to an API endpoint using curl and Python?

Answer: Multipart form uploads use -F in curl and files= in requests; set content-type correctly to avoid server-side parsing errors.

Why this approach: Streaming the file handle directly (data=fh) avoids loading the entire file into memory, which is essential for large artifacts like VM images or database dumps.


Q134: How do you parse and transform XML API responses in Python?

Answer: xml.etree.ElementTree is in the standard library and handles namespaced XML; findall with XPath subsets extracts nodes efficiently.

Why this approach: Registering namespace prefixes in a dictionary and passing it to findall/findtext is the correct way to handle namespaced XML, since XPath requires full namespace URIs which are too verbose to repeat inline.


Q135: How do you generate a signed request (HMAC) for an API in Python?

Answer: HMAC signing involves canonicalizing request components, signing with the shared secret, and attaching the signature as a header—the same pattern used by AWS SigV4 and many webhook providers.

Why this approach: Including a timestamp in the canonical string prevents replay attacks (the server can reject requests older than 5 minutes), and hashing the body detects payload tampering in transit.


Real-World Automation Scenarios

Q136: How do you write a script to rotate logs with date-stamped archives and retention?

Answer: A production log-rotation script must handle the live file safely, compress archives efficiently, and prune old files based on a retention policy.

Why this approach: truncate -s 0 is the standard way to clear a live log file because it modifies the file in place without changing its inode, so any process with an open file descriptor continues writing to the same file and does not lose log entries.


Q137: How do you bulk SSH to N servers with parallel execution?

Answer: xargs -P or GNU parallel distributes SSH commands across multiple hosts simultaneously; capturing per-host output separately prevents interleaving.

Why this approach: asyncssh with asyncio.Semaphore is more efficient than threads for I/O-bound SSH connections because it avoids thread-per-connection overhead while still limiting concurrency to avoid overwhelming the target hosts.


Q138: How do you write an idempotent user provisioning script?

Answer: Idempotency means running the script twice produces the same result as running it once; check existence before create, use || to skip on already-exists, and validate the final state.

Why this approach: Checking state before acting (id, grep -qF) ensures the script is a no-op on subsequent runs, making it safe for cron, CI, and Terraform provisioners that may execute it repeatedly.


Q139: How do you write a backup script with a retention policy (keep last 7 days)?

Answer: The script must create a timestamped backup atomically, verify its integrity, and prune old backups based on count or age.

Why this approach: Verifying the archive with tar --list before writing the checksum means the checksum file is only created for known-good archives, preventing silent corruption from disk errors during write.


Q140: How do you write a deploy script with pre-flight checks and rollback?

Answer: A production deploy script must validate prerequisites, deploy atomically (symlink swap), and roll back automatically if smoke tests fail.

Why this approach: The atomic mv -Tf symlink swap ensures the new release is either fully live or not at all—there is no window where current points to a partially-extracted directory.


Q141: How do you find and kill zombie processes?

Answer: Zombie processes are already dead—they have no code to kill; only reaping them (having the parent call wait) removes them. The script identifies zombies and their parents for operator action.

Why this approach: Sending SIGCHLD to the parent is the correct mechanism—it prompts the parent to call wait() and collect the zombie's exit status, which is what removes the process table entry.


Q142: How do you write a service watchdog that restarts a service if it goes down?

Answer: A watchdog polls a health endpoint or process table and restarts the service if it fails, with alerting and a back-off to prevent restart loops.

Why this approach: The sliding window counter (restart_count / window_start) distinguishes between a service that fails once versus one in a crash loop, preventing the watchdog from repeatedly restarting a persistently broken service and masking the real problem.


Q143: How do you use shellcheck and fix common Bash pitfalls it identifies?

Answer: shellcheck is a static analysis tool for Bash; running it in CI prevents entire categories of bugs before they reach production.

Why this approach: Integrating shellcheck into CI with --severity=warning catches 90% of Bash correctness issues automatically, turning code review from bug-hunting into logic review.


Q144: How do you unit-test Bash scripts with bats?

Answer: bats (Bash Automated Testing System) provides a @test DSL with assert_output, assert_failure, and setup/teardown hooks for testing Bash functions in isolation.

Why this approach: bats runs each @test in a subshell so setup state does not leak between tests, and the assert_* helpers produce readable diff output on failure rather than cryptic exit code mismatches.


Q145: How do you unit-test Python automation scripts with pytest?

Answer: pytest with monkeypatch and tmp_path lets you test scripts that interact with the filesystem, environment variables, and external commands without touching real resources.

Why this approach: monkeypatch.setattr replaces real system calls (psutil, requests) with controlled stubs, making tests deterministic and fast without needing a real network or filesystem state.


Q146: How do you debug a Bash script using set -x, bash -n, and trap DEBUG?

Answer: bash -n syntax-checks without running; set -x traces execution; trap DEBUG gives per-command hooks for custom logging.

Why this approach: Setting PS4 with ${BASH_SOURCE}:${LINENO} makes set -x output include file and line number, turning the trace from an unstructured dump into a navigable log.


Q147: How do you write a script that is safe to run twice (idempotent patterns)?

Answer: Idempotency requires testing preconditions before acting and using atomic operations; the patterns are consistent across file creation, directory setup, service configuration, and database migrations.

Why this approach: The mv -Tf symlink pattern is atomic—the old link is replaced in one syscall so concurrent readers always see either the old or new target, never a dangling intermediate state.


Q148: What are the most dangerous Bash pitfalls involving word splitting, globbing, and quoting?

Answer: These three features interact to cause subtle bugs that work in testing but fail silently in production with real filenames, empty variables, or unexpected characters.

Why this approach: Adding set -euo pipefail to every script and always quoting variable expansions catches the majority of these pitfalls at the cost of minor extra typing, while the specific patterns above address the cases where quoting alone is insufficient.


Q149: How do you use set -x, bash -n, and trap DEBUG together for comprehensive debugging?

Answer: A debug harness combines all three techniques to produce a structured trace with timestamps, function names, and line numbers that can be enabled/disabled by an environment variable.

Why this approach: Writing xtrace to a separate file descriptor with BASH_XTRACEFD keeps the debug trace out of stdout so it does not corrupt piped output, while the ERR trap with caller provides a human-readable stack trace without requiring a debugger.


Q150: When should you use Bash vs Python vs Go for a script/tool?

Answer: Language choice is an engineering decision based on complexity, performance, portability, maintenance burden, and the skill level of people who will need to modify the code later.

Why this approach: Choosing the right tool is itself an interview signal — demonstrating that you reach for Python when Bash will create maintenance debt, and Go when distribution or performance matters, shows systems engineering judgment beyond syntax knowledge.


Quick Reference Cheat Sheet

Health Check & Monitoring

curl One-liners

jq One-liners

Retry & Rate Limit

Parallelism

Idempotency Patterns

Debugging

Language Selection Summary

TaskBashPythonGo
Wrap CLI tools✓✓✓-
Parse JSON/CSV-✓✓✓
HTTP API calls✓✓✓✓
Parallel work✓ (xargs)✓✓✓
Single binary--✓✓
Unit testsbats (ok)✓✓✓✓
> 200 linescaution✓✓✓✓
Cross-platform-✓✓✓✓