Data Validation
Required Field
Reject Empty Text
A required field should be checked before the script builds commands or summaries from it.
Program
Play the script to choose a field value and see the validation message.
required_field.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
owner="team-a"
if [ -n "$owner" ]; then
message="owner=$owner"
else
message="missing-owner"
fi
echo "$message"
#!/usr/bin/env bash
owner=""
if [ -n "$owner" ]; then
message="owner=$owner"
else
message="missing-owner"
fi
echo "$message"
owner ← team-a
3owner="team-a"4if [ -n "$owner" ]; thenvalues this stepteam-aownerif [ -n "$owner" ]; then
3owner="team-a"4if [ -n "$owner" ]; then5 message="owner=$owner"values this stepteam-aownermessage ← owner=team-a
4if [ -n "$owner" ]; then5 message="owner=$owner"6elsevalues this stepowner=team-amessageteam-aownerecho "$message"
8fi9echo "$message"outputowner=team-avalues this stepowner=team-amessage
owner ← (empty)
3owner=""4if [ -n "$owner" ]; thenvalues this step(empty)ownerif [ -n "$owner" ]; then
3owner=""4if [ -n "$owner" ]; then5 message="owner=$owner"values this step(empty)ownermessage ← missing-owner
6else7 message="missing-owner"8fivalues this stepmissing-ownermessageecho "$message"
8fi9echo "$message"outputmissing-ownervalues this stepmissing-ownermessage
required field
A required value must be present before the script continues.
non-empty test
`[ -n "$owner" ]` checks that the string has length.
error message
A named failure message gives callers a clear correction.