0

I've got a case:

case $1 in
  "start") exec docker-compose up ;;
  "check-types") docker-compose run --rm myapp mypy --config-file=mypy.ini $2;;
  "help" | *) print-help ;;
esac

I would like to replace $2 written in "check-types" with an if statment which would state something similar to:

if [[ $2 ]]; then
  $2
else
  .
fi

Is that possible in one line of bash?

Example of the desired behavior:

  • writing ./cli check-types executes docker-compose run --rm myapp mypy --config-file=mypy.ini .
  • writing ./cli check-types app/test executes docker-compose run --rm myapp mypy --config-file=mypy.ini app/test
0

1 Answer 1

4

You can use the :- operator to specify a default value if $2 is null or unset.

case $1 in
  "start") 
    exec docker-compose up ;;
  "check-types")
    docker-compose run --rm myapp mypy --config-file=mypy.ini "${2:-.}";;
  "help" | *)
    print-help ;;
esac

(In general, though, bash does not have a conditional expression.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.