Interview › Scripting (Bash, Groovy)
How do you write a dry-run mode into an automation script, and why is it valuable? [Advanced]
Answer
Dry-run mode shows what the script would change without actually changing it. It is valuable because operators can review planned actions, CI can validate behavior safely, and risky automation can be tested before production execution.
Technical explanation
Implement dry-run by wrapping mutating commands in a function that either prints or executes them.
Read-only validation should still run during dry-run so missing prerequisites are caught.
The output should be explicit enough for review, including target environment, resources, and commands or API actions that would run.
Hands-on example
dry_run=false
[[ "${1:-}" == "--dry-run" ]] && dry_run=true
run_cmd() {
if $dry_run; then
printf 'DRY-RUN: '
printf '%q ' "$@"
printf '\n'
else
"$@"
fi
}
run_cmd kubectl -n prod rollout restart deployment/orders-api
run_cmd kubectl -n prod rollout status deployment/orders-api --timeout=5m
Source Notes
GNU Bash Reference Manual - shell expansions: https://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html
GNU Bash Reference Manual - shell parameters: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html
GNU Bash Reference Manual - top/reference: https://www.gnu.org/software/bash/manual/html_node/index.html
Apache Groovy documentation: https://groovy-lang.org/documentation.html
Apache Groovy closures: https://groovy-lang.org/closures.html
Jenkins Pipeline syntax: https://www.jenkins.io/doc/book/pipeline/syntax/
Jenkins Pipeline getting started: https://www.jenkins.io/doc/book/pipeline/getting-started/
Jenkins Pipeline CPS method mismatches: https://www.jenkins.io/doc/book/pipeline/cps-method-mismatches/
Jenkins Pipeline steps reference - Groovy: https://www.jenkins.io/doc/pipeline/steps/groovy/
Jenkins Pipeline: Groovy plugin steps: https://www.jenkins.io/doc/pipeline/steps/workflow-cps/
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- What is the purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with ./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]