22

I'm trying to make a getopt command such that when I pass the "-ab" parameter to a script, that script will treat -ab as a single parameter.

#!/bin/sh
args=`getopt "ab":fc:d $*`
set -- $args
for i in $args
do
case "$i" in
        -ab) shift;echo "You typed ab $1.";shift;;
        -c) shift;echo "You typed a c $1";shift;;
esac
done

However, this does not seem to work. Can anyone offer any assistance?

5 Answers 5

17

getopt doesn't support what you are looking for. You can either use single-letter (-a) or long options (--long). Something like -ab is treated the same way as -a b: as option a with argument b. Note that long options are prefixed by two dashes.

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

1 Comment

Could you then show us how to do it with --long then?
3

i was struggling with this for long - then i got into reading about getopt and getopts

single char options and long options .

I had similar requirement where i needed to have number of multichar input arguments.

so , i came up with this - it worked in my case - hope this helps you

function show_help {
    echo "usage:  $BASH_SOURCE --input1 <input1> --input2 <input2> --input3 <input3>"
    echo "                     --input1 - is input 1 ."
    echo "                     --input2 - is input 2 ."
    echo "                     --input3 - is input 3 ."
}

# Read command line options
ARGUMENT_LIST=(
    "input1"
    "input2"
    "input3"
)



# read arguments
opts=$(getopt \
    --longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
    --name "$(basename "$0")" \
    --options "" \
    -- "$@"
)


echo $opts

eval set --$opts

while true; do
    case "$1" in
    h)
        show_help
        exit 0
        ;;
    --input1)  
        shift
        empId=$1
        ;;
    --input2)  
        shift
        fromDate=$1
        ;;
    --input3)  
        shift
        toDate=$1
        ;;
      --)
        shift
        break
        ;;
    esac
    shift
done

Note - I have added help function as per my requirement, you can remove it if not needed

2 Comments

-h is not showing help
Hello , for my case it is custom echo , you can try using --help if that helps
2

That's not the unix way, though some do it e.g. java -cp classpath.

Hack: instead of -ab arg, have -b arg and a dummy option -a.

That way, -ab arg does what you want. (-b arg will too; hopefully that's not a bug, but a shortcut feature...).

The only change is your line:

-ab) shift;echo "You typed ab $1.";shift;;

becomes

-b) shift;echo "You typed ab $1.";shift;;

Comments

1

GNU getopt have --alternative option

-a, --alternative
    Allow long options to start with a single '-'.

Example:

#!/usr/bin/env bash

SOPT='a:b'
LOPT='ab:'
OPTS=$(getopt -q -a \
    --options ${SOPT} \
    --longoptions ${LOPT} \
    --name "$(basename "$0")" \
    -- "$@"
)

if [[ $? > 0 ]]; then
    exit 2
fi

A= 
B=false
AB=

eval set -- $OPTS

while [[ $# > 0 ]]; do
    case ${1} in
        -a)  A=$2 && shift   ;;
        -b)  B=true          ;;
        --ab) AB=$2 && shift ;;
        --)                  ;;
        *)                   ;;
    esac
    shift
done

printf "Params:\n    A=%s\n    B=%s\n    AB=%s\n" "${A}" "${B}" "${AB}"
$ ./test.sh -a aaa -b -ab=test
Params:
    A=aaa
    B=true
    AB=test

Comments

-5

getopt supports long format. You can search SO for such examples. See here, for example

1 Comment

Can you please offer an example of how this is used?

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.