B
B
bozuriciyu2019-10-02 01:04:33
linux
bozuriciyu, 2019-10-02 01:04:33

How to check for the presence of arguments and abort the script?

In a simple script, I am passing 3 arguments

project_slug=${1}
back_port=${2}
front_port=${3}

How to check the presence of each argument, and if it is missing, abort the script (well, display a message why)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2019-10-02
@bozuriciyu

for example like this:

if [ $# -eq 0 ]; then
  echo "No arguments"
  exit
fi

if [ -z "$2" ]; then
  echo "No argument 2"
fi

if [ -z "$3" ]; then
  echo "No argument 3"
  exit
fi

echo "Run script"

can be written better, but as a starting point, this option will go.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question