0

The question is very simple. I was reading the man page of getopts but I see no description on how the opstring should be. So, how should I know? I know I can Google it but I want to know how to get the answer from the documentation.

VLAZ
  • 26,331
  • 9
  • 49
  • 67

1 Answers1

2

When the documentation for a shell builtin function is murky, the best solution tends to be to find an understandable example. Grepping /bin/* shows many uses (though since my system has a lot of developer packages, it might have more than normal).

/bin/smistrip (at line 153) uses it like this:

while getopts Vhnm:i:d: c ; do
    case $c in
        n)      test=1
                ;;
        m)      single=$OPTARG
                ;;
        i)      indir=$OPTARG
                ;;
        d)      dir=$OPTARG
                ;;
        h)      do_usage
                exit 0
                ;;
        V)      do_version
                exit 0
                ;;
        *)      do_usage
                exit 1
                ;;
    esac
done

This is very similar to how getopt() works from C. See man 3 getopt for a clear example and explanation.

wallyk
  • 56,922
  • 16
  • 83
  • 148