1

I have the shell script

CONTROLLER_IP=""
if [ "$#" -eq 1 ]
then
    CONTROLLER_IP=$1
else
    echo "Usage : create_endpoint.sh --controller-ip <Controller IP>"
    exit 1
fi

but its executing like this ./create_endpoint.sh 10.10.10.1

I want to execute like this /create_endpoint.sh --controller-ip 10.10.10.1

2
  • So are you trying to set it so that it accepts an option of --controller-ip rather than just typing the IP as the first argument?
    – Arronical
    Jul 3, 2015 at 8:56
  • yes. I need like that
    – Beginner
    Jul 3, 2015 at 9:27

3 Answers 3

4

Something like this:

#!/bin/bash
CONTROLLER_IP=""

while [ "$1" != "" ]; do
  case $1 in
    -c|--controller-ip)
      if [ -n "$2" ]; then
        CONTROLLER_IP="$2"
        shift 2
        continue
      else
        echo "ERROR: '--controller-ip' requires a non-empty option argument."
        exit 1
      fi
        ;;
    -h|-\?|--help)
      echo "Usage: $(basename $0) --controller-ip <ip>"
      exit
      ;;
    --)              # End of all options.
      shift
      break
      ;;
    -?*)
      echo "WARN: Unknown option (ignored): $1"
      ;;
    *)               # Default case: If no more options then break out of the loop.
      break
  esac
  shift
done

echo "$CONTROLLER_IP"

Examples

$ ./foo --controller-ip 192.168.2.1
192.168.2.1

$ ./foo -c 192.168.2.1
192.168.2.1

$ ./foo --controller-ip
ERROR: '--controller-ip' requires a non-empty option argument.

$ ./foo --help
Usage: foo --controller-ip <ip>

$ ./foo -help
WARN: Unknown option (ignored): -help

As OP commented:

I don't want help option.

#!/bin/bash
CONTROLLER_IP=""

while [ "$1" != "" ]; do
  case $1 in
    -c|--controller-ip)
      if [ -n "$2" ]; then
        CONTROLLER_IP=$2
        shift 2
        continue
      else
        echo "ERROR: '--controller-ip' requires a non-empty option argument."
        exit 1
      fi
        ;;
    --)              # End of all options.
      shift
      break
      ;;
    -?*)
      echo "WARN: Unknown option (ignored): $1"
      ;;
    *)               # Default case: If no more options then break out of the loop.
      break
  esac
    shift
done

echo "$CONTROLLER_IP"
8
  • I don't want help option.
    – Beginner
    Jul 3, 2015 at 9:32
  • @Ubuntu-Beginner Take the 2nd version.
    – A.B.
    Jul 3, 2015 at 9:36
  • if i have multiple arguments. how can I do that
    – Beginner
    Jul 3, 2015 at 11:08
  • @Ubuntu-Beginner In the same way as for -c|--controller-ip)
    – A.B.
    Jul 3, 2015 at 11:09
  • @Ubuntu-Beginner Don't thank me! ;-) If my answer is correct, just click the little grey under the "2" now turning it into beautiful green. If you do not like my answer, click on the little grey down-arrow below the 2, and if you really like my answer, click on the little grey checkmark and the little up-arrow... If you have any further questions, go to askubuntu.com/questions/ask
    – A.B.
    Jul 3, 2015 at 11:13
4

The getopt command is used for parsing options in bash scripts there's a great guide to its use here.

You can set long or short options, and also specify whether the option has required or optional arguments with use of : or ::.

Don't confuse it with the bash builtin getopts which can only read short options, and processes slightly differently.

I found this guide to the differences between the two simple and straightforward.

1

It is much better to use a code generator such as Argbash, that will generate the argument handling code for you. There are also universal bash libraries for argument parsing, but you would have to distribute them with your script, and they are quite long and complex.

Why?

  • The answer suggesting using getopt is not a good choice - its support of long options is a GNU extension, so scripts using it are not portable. Plus getopt has its own issues besides that.
  • The answer suggesting writing bash argument parsing code also has an issue - maintaining relatively long hand-written code like this doesn't feel right.

Finally, arguments that begin with dashes are commonly known as optional and treated as such - they should not be required and if they are omitted, a sensible default should be available. Therefore, the original script you are somehow unhappy about, may actually be the best solution.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .