A deploy guard that passes with no token

A deploy guard that passes with no token

●2 ●41 ●103
calendar_today • schedule1 min read

Five lines whose only job is to refuse a deploy when the token is missing. Run with the token unset:

$ cat deploy-guard.sh
#!/bin/bash
if [ -n $DEPLOY_TOKEN ]; then
  echo "token present, deploying"
else
  echo "no token, refusing" >&2; exit 1
fi
$ env -u DEPLOY_TOKEN bash deploy-guard.sh
token present, deploying
exit=0

No error, exit 0. An unquoted empty variable is removed by word splitting, so [ receives [ -n ]: one argument, and a one-argument test is true for any non-empty string. Here that string is the literal -n.

Put a binary operator after the same empty variable and it fails loudly instead: [ $count -eq 1 ] becomes [ -eq 1 ] and prints unary operator expected with status 2, which an if treats as false. A value with a space fails the other way, too many arguments.

Quoting fixes both, [ -n "$DEPLOY_TOKEN" ], and ShellCheck flags the original as SC2070, rated an error. Better still, stop on a missing value before any test runs: : "${DEPLOY_TOKEN:?DEPLOY_TOKEN must be set}". The same expression under [, quotes, [[, a default and set -u, side by side, is in Fix "unary operator expected" in Bash.


I bundled the scripts I copy onto every new server, plus 10 bonus ones, into the Production Bash Toolkit. 30-day refund if it isn't useful.

Want more #devops & #devops deep dives?

Join 6,151+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

3 Comments

1 vote
0
0

More Posts

The cleanup failed on the night there was nothing to clean

BashSnippets - Dec 2

My hung process had no file open. It was stuck inside open().

BashSnippets - Dec 1

Your AI Agent Doesn't Need Your Token: Edera's Ariadne Conill on Object Capabilities

Tom Smithverified - Nov 5

SC2115: the empty variable that turns rm -rf "$DIR/"* into rm -rf /*

BashSnippets - Nov 19

ShellCheck rates SC2086 as info. It let rm delete two files I never named.

BashSnippets - Sep 29
chevron_left
4k Points • 146 Badges
North America • bashsnippets.xyz
79Posts
44Comments
14Connections
Linux user who got tired of Googling the same bash commands every time I sat down at a terminal. Sta... Show more

Related Jobs

View all jobs →

Commenters (This Week)

8 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!