busybox getopt ignores 1st positional paramater

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
s243a
Posts: 2580
Joined: Tue 02 Sep 2014, 04:48
Contact:

busybox getopt ignores 1st positional paramater

#1 Post by s243a »

Some code:

Code: Select all

  declare -a options
  while read option; do
    options+=( "$option" )
  done < <(getopt --long no-strip::,no-self:: -- "$@")
  declare -a self
  eval set -- "${options[@]}"
**Note the loop isn't strictly necessary. The following is likely also okay:

Code: Select all

  declare -a options
  options="$(getopt --long no-strip::,no-self:: -- "$@")"
  declare -a self
  eval set -- "${options[@]}"
but the first code lets me see better what getopt is actually returning.

Some mysterious output:

Code: Select all

+++ getopt --long no-strip::,no-self:: -- libc6 /tmp/pkg/root/pkg_aliases
++ options+=("$option")
++ read option
++ declare -a self
++ eval set -- '-- '\''/tmp/pkg/root/pkg_aliases'\'''
The mystery here is what happened to the positional parameter libc6.

Trying to work this issue out I created some test code that I typed direectly into a terminal:

Code: Select all

# unset options
# while read option; do options+=($option); done < <(getopt -s bash --long no-strip::,no-self:: -- '' 'libc6' '/tmp/pkg/root/pkg_aliases' 'libc7')
# bla="${options[@]}"
# echo $bla
-- 'libc6' '/tmp/pkg/root/pkg_aliases' 'libc7'
Adding a dummy argument (i.e. '' ) after the double dash seems to fix the issue but I'm not sure if this would be compatible with a full version of getopt. I'm wondering if I"m dealing with some wierd busybox quirk here. The issue doesn't manifest if I only have one positional parameter. I wonder if the quirk here is the way that busybox treats the double dash (i.e. '--')
Find me on [url=https://www.minds.com/ns_tidder]minds[/url] and on [url=https://www.pearltrees.com/s243a/puppy-linux/id12399810]pearltrees[/url].

Burunduk
Posts: 80
Joined: Sun 21 Aug 2011, 21:44

#2 Post by Burunduk »

You are not the only one asking this question:

https://unix.stackexchange.com/question ... t-argument
s243a wrote:Adding a dummy argument (i.e. '' ) after the double dash seems to fix the issue but I'm not sure if this would be compatible with a full version of getopt.
Yes.
s243a wrote:I'm wondering if I"m dealing with some wierd busybox quirk here.
No, util-linux getopt behaves the same way.

This is not unusual. sed treats its first non-option argument as a program not as a file name. getopt uses it as a short options string that may be empty. Another place for that empty string is after the -o option:

Code: Select all

busybox getopt -s bash --long no-strip::,no-self:: -o '' -- 'libc6' '/tmp/pkg/root/pkg_aliases' 'libc7'

BTW, this is what the busybox source says about getopt:
getopt.c wrote:The getopt utility is used to break up (parse) options in command
lines to make it easy to write complex shell scripts that also check
for legal (and illegal) options. If you want to write horribly
complex shell scripts, or use some horribly complex shell script
written by others, this utility may be for you. Most people will
wisely leave this disabled.

Post Reply