Subjectively

dd if=/dev/random | kirk > blog

Subjectively header image 2

bash shell script to use getopts with gnu style long positional parameters

October 29th, 2009 · 1 Comment · Linux, OS X

Problem: You want to support –really-long-option-names in your bash script and getopts won’t do it.
More problems: You don’t want to use getopt because you want cross platform support or your platform isn’t supported.
More problems than that: I can’t help with existential issues.
Solution: Rewrite the long options into short options and then use getopts.

./args.sh -b -c -a “yada yada” –some-other-gnu –long-gnu-option

content of args.sh:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
for arg
do
    delim=""
    case "$arg" in
    #translate --gnu-long-options to -g (short options)
        --some-other-gnu) args="${args}-g ";;
       --long-gnu-option) args="${args}-l ";;
       #pass through anything else
       *) [[ "${arg:0:1}" == "-" ]] || delim="\""
           args="${args}${delim}${arg}${delim} ";;
    esac
done
 
#Reset the positional parameters to the short options
eval set -- $args
 
while getopts ":a:gl" option 2>/dev/null
do
    case $option in
        g) echo "some other gnu option";;
        l) echo "long gnu option";;
        a) echo ${OPTARG[@]};;
        *) echo $OPTARG is an unrecognized option;;
    esac
done

Output:

b is an unrecognized option
c is an unrecognized option
yada yada
some other gnu option
long gnu option

Tags:

One Comment so far ↓

Leave a Comment

You must log in to post a comment.