Skip to content
$ lynuxpro

INTERVIEW PREP

Interview questions

570 questions from real DevOps, SRE, platform engineering and MLOps interviews, with the answer an interviewer wants to hear. Try answering out loud first, then open the model answer.

Showing 31–45 of 314 questions

31.How do you handle errors so the script stops and tells you where?MidBash Scripting

Use set -euo pipefail, plus trap 'echo "error on line $LINENO" >&2' ERR. Check commands whose failure is OK explicitly (cmd || true) and send error messages to stderr.

32.How do you loop over files whose names may have spaces?MidBash Scripting

Use a glob, not ls: for f in /data/*.csv; do process "$f"; done. With find, use find . -name '*.log' -print0 | while IFS= read -r -d '' f; do ...; done.

33.How do you parse options like -v and -f file in a script?MidBash Scripting

Use the getopts builtin: while getopts 'vf:' opt; do case $opt in v) verbose=1;; f) file=$OPTARG;; *) usage;; esac; done. Then shift $((OPTIND-1)) leaves the remaining arguments.

34.What is a here document, and when would you use one?MidBash Scripting

A here document feeds several lines to a command: cat <<'EOF' > config.yml ... EOF. Quoting 'EOF' stops variable expansion, which is handy for writing config files or sending a block of commands over SSH.

35.How do you prevent two copies of a cron script running at once?SeniorBash Scripting

Use a lock: exec 9>/var/lock/job.lock; flock -n 9 || exit 0. The kernel releases the lock when the process exits, even if it crashes, unlike a hand-made PID file.

36.How do you test and lint Bash scripts in CI?SeniorBash Scripting

Run ShellCheck on every script, write tests with bats-core, run with bash -n for syntax, and test under the shell that will run it (dash for /bin/sh scripts).

37.When should you stop using Bash and switch to Python?SeniorBash Scripting

Switch when the script needs real data structures, JSON/YAML parsing, error handling with retries, unit tests, or grows past roughly 100-200 lines. Bash is great for gluing commands together; Python is easier to read, test and maintain for logic.

38.How do you run commands in parallel in Bash and wait for them?SeniorBash Scripting

Start them with & and call wait; capture each PID to check exit codes (wait $pid). For many jobs, xargs -P 8 or GNU parallel limits how many run at once.

39.How do you make a script safe to run twice (idempotent)?SeniorBash Scripting

Check state before changing it: mkdir -p, id user || useradd user, grep -q line file || echo line >> file. Use lock files (flock) to stop overlapping runs, and write to a temp file then mv it into place so a crash never leaves half a file.

40.How do you keep secrets out of shell scripts and logs?SeniorBash Scripting

Read secrets from environment variables or a secret manager at runtime, never hardcode them. Avoid set -x around secret lines, don't pass secrets as command-line arguments (they show in ps), and use read -s for prompts.

41.Why use a virtual environment?BeginnerPython for DevOps

It gives each project its own Python packages, so versions never clash between projects and your system Python stays clean.

42.What is the difference between a list and a dict?BeginnerPython for DevOps

A list is an ordered group of items you reach by position. A dict stores values under names (keys), so you reach them by key.

43.How do you read a file safely?BeginnerPython for DevOps

Use a with block: with open(path) as f. It closes the file even when an error happens. pathlib makes paths work on every system.

44.What does exit code 0 mean?BeginnerPython for DevOps

Success. Any other number means failure. CI systems and shells read this number, so scripts should exit non-zero when they fail.

45.Why is shell=True dangerous in subprocess?MidPython for DevOps

The text is parsed by a shell, so special characters from outside input can run extra commands. Pass the command as a list of arguments instead.