Pcornel
Automation · Scripting

Command Execution

Cheat sheet · https://pcornel.dk/topic.php?topic=5226

Back to the Topic

Answer

Command execution is when a script runs an external program or OS command. Do it safely by passing arguments as a list (not a shell string), setting the working directory and timeout, and checking the exit code and output.

Remember

  • Command started ≠ command completed correctly; process creation proves only the execution boundary began.
  • Exit code 0 ≠ target state verified; external tools may report success before asynchronous work completes.
  • Quoted string ≠ safe command construction automatically; shell interpolation and platform quoting rules can still reinterpret input.
  • Captured stdout ≠ full result; stderr, exit status, timeout, signals and target-side state can carry the actual failure.

First checks

  • shellcheck script.sh — quoting issues
  • subprocess.run([...], check=True) (Python) — no shell
  • Start-Process / & with argument arrays — PowerShell

Verify in this order

script → command with args → exit code + output

Where it usually breaks

Process creation or even exit code zero proves only the external command's process result; it does not prove asynchronous or remote side effects reached the intended state.

Script/input → command/module binding → local/remoting session → request accepted → command result → target object read-back → effective runtime state → workload.

Command Execution scripting boundary map

BoundaryHealthy evidenceSuspicious evidence
Input / runtimeEffective inputs, runtime, working context and dependency versions are explicitHidden defaults, stale environment, wrong interpreter/path or unvalidated input
Logic / executionBranch/function/module decisions and external actions expose per-operation resultsSwallowed failures, shell reinterpretation, blind retries or last-result-wins behavior
Result / proofExit/exception/log evidence is correlated with independent target read-backProcess success or printed output treated as target success
Recovery / troubleshootingFirst failed boundary is preserved, changed minimally and retestedRandom edits, catch-all suppression or evidence destroyed before diagnosis

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5226

Confirmation

Confirm action

Continue?