Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a multi-character parameter in UNIX using getopt?

Tags:

unix

getopt

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?

like image 436
Waffles Avatar asked Oct 20 '10 05:10

Waffles


People also ask

How does getopt work in Unix?

Description. The getopts command is a Korn/POSIX Shell built-in command that retrieves options and option-arguments from a list of parameters. An option begins with a + (plus sign) or a - (minus sign) followed by a character. An option that does not begin with either a + or a - ends the OptionString.

Should I use getopt or getopts?

The main differences between getopts and getopt are as follows: getopt does not handle empty flag arguments well; getopts does. getopts is included in the Bourne shell and Bash; getopt needs to be installed separately. getopt allows for the parsing of long options ( --help instead of -h ); getopts does not.

What is Optarg getopt?

optarg indicates an optional parameter to a command line option. opterr can be set to 0 to prevent getopt() from printing error messages. optind is the index of the next element of the argument list to be process, and optopt is the command line option last matched.


2 Answers

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.

like image 150
paprika Avatar answered Oct 23 '22 22:10

paprika


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

like image 23
Ashish Shetkar Avatar answered Oct 23 '22 21:10

Ashish Shetkar