Reading a script before you run it
12 minRead before you run
What to look for in a script somebody else wrote.
This is the track the rest exists for. A model's output is executed directly against your machine with your privileges, usually pasted without being read — and the thing that makes reading it possible is knowing the expansion order.
Four questions get you most of the way. Which paths does it touch? What happens if a variable it uses is empty? What happens if a filename contains a space? And does anything it runs delete, overwrite or change permissions?
Try this
The first question is the sharpest, because the shell's answer is not the obvious one. An unquoted empty variable does not become an empty argument — it disappears entirely, and the command sees one fewer argument than the author intended.
rm -rf "$DIR/" with DIR unset becomes rm -rf / in a shell without set -u, and rm -rf $DIR/* becomes rm -rf /*. Neither reports anything unusual.
Try this
set -u turns that into an error before it turns into a morning. It is one line, and it is why generated scripts should start with it.
The audit exercises give you a script an agent produced, a filesystem that looks like somebody's home directory, and one question asked before it runs: what will this do? Then you run it and watch.
Files
| Mode | Links | Size | Name |
|---|---|---|---|
| -rw-r--r-- | 1 | 35 | .bashrc |
| drwxr-xr-x | 2 | drafts/ | |
| -rw-r--r-- | 1 | 68 | old-invoice.txt |
| drwxr-xr-x | 2 | inbox/ | |
| -rwxr-xr-x | 1 | 32 | build.sh |
| -rw-r--r-- | 1 | 126 | notes.txt |
| drwxr-xr-x | 2 | scratch/ | |
| -rw-r--r-- | 1 | 77 | data.csv |
| -rw-r--r-- | 1 | 189 | report.txt |
| -rw-r--r-- | 1 | 112 | todo.md |