Turn warnings in errors (or in exit-code different from zero)

+ tree
.
├── compose.yml
└── my-script



+ cat my-script
docker compose run --rm test

ec=$?

echo Exit code: $ec

if [ $ec -eq 0 ]; then
    echo No errors found
fi



+ cat compose.yml
services:
    test:
        image: phpdoc/phpdoc:3
        user: "${UID}:${GID}"



+ sh -u my-script
WARN[0000] The "UID" variable is not set. Defaulting to a blank string. 
WARN[0000] The "GID" variable is not set. Defaulting to a blank string. 
blah blah blah
Exit code: 0
No errors found

Let’s look at things in more detail

WARN[0000] The "UID" variable is not set. Defaulting to a blank string. 
WARN[0000] The "GID" variable is not set. Defaulting to a blank string. 

OK, is compressible

Exit code: 0

mmmhhh…

No errors found

No errors found

In short…

As per the subject, is there a solution?

Not sure if you are aware, we are humans here, not chatbots. We like to be treated politely. We like to get some context to understand better.

What are you trying to achieve, how is your setup, what did you try, what did not work?

I think with a less robotic mostly code post, you might get more elaborate answers.

Like this it is way more user friendly to read, habe different files and code in different boxes.

compose.yml:

services:
    test:
        image: phpdoc/phpdoc:3
        user: "${UID}:${GID}"
1 Like

I would like, in case of error/warning

e.g.

WARN[0000] The "UID" variable is not set.......

the command

docker compose ...

can generate a non-zero exit-code

This will produce a none zero exit code (1) if either are unset.

#!/bin/sh
set -eu

# Manually enforce presence of UID and GID
: "${UID:?Environment variable UID is not set}"
: "${GID:?Environment variable GID is not set}"

docker compose run --rm test
ec=$?

echo "Exit code: $ec"
if [ "$ec" -eq 0 ]; then
    echo "No errors found"
fi
1 Like

What is the colon for?
What is the quote for?
What does :? ?

  • : is the no-op command (a built-in shell command that does nothing and always returns true). It’s used here just to trigger the expansion.
  • ${VAR:?message} checks whether the environment variable is set and non-null.
  • If VAR is unset or null, the shell prints message to stderr and exits the script with a non-zero status.
  • If VAR is set and non-empty, the expression expands to its value and : just ignores it.

ok but I asked three questions, and there is no answer

Nothing?

**** This topic will close a month after the last reply.***

OK, let’s break it down:

  1. set -eu
  • e: This option causes the script to exit immediately if any command fails (this returns a non-zero exit code).
  • u: This option treats unset variables as an error when performing parameter expansion. The script will exit if it tries to use an undefined variable.
  1. : "${UID:?Environment variable UID is not set}"
  • This is a form of parameter expansion.
  • If the environment variable UID is not set or is null, the script will print the message “Environment variable UID is not set” to standard error and exit with a non-zero status (due to set -u and set -e).
  • If UID is set, the : (colon) command (which is a built-in no-op that successfully does nothing) is executed, and the script continues.
  1. : "${GID:?Environment variable GID is not set}"
  • This works exactly like the UID check above but for the GID environment variable. If GID is not set, the script prints the specified error and exits with a non-zero status.
  1. docker compose run --rm test
  • This command attempts to run a service named test defined in your docker-compose.yml file.
  • If this command fails for any reason, it will return a non-zero exit code. Reasons for failure could include:
    • The docker-compose.yml file is missing or misconfigured.
    • The service test is not defined.
    • The Docker image for the test service cannot be built or pulled.
    • The container itself exits with a non-zero status during its execution.
  • Because set -e is active, if docker compose run fails, the script will immediately exit at this point with that non-zero exit code.
  1. ec=$?
  • This line captures the exit code of the immediately preceding command (docker compose run --rm test) and stores it in the variable ec.
  1. echo "Exit code: $ec"
  • This line will print the captured exit code.
  1. if [ "$ec" -eq 0 ]; then echo "No errors found"; fi
  • This conditional statement checks if the exit code stored in ec is equal to 0.
  • If ec is not 0 (meaning an error occurred in docker compose run), the message “No errors found” will not be printed.

Difference…?

I’m done. Figure it out.

Hey

That user is most probably a bot, don’t loose your time anymore and ignore it.