Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sun 15 Dec 2019, 03:41
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
busybox getopt ignores 1st positional paramater
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [2 Posts]  
Author Message
s243a

Joined: 02 Sep 2014
Posts: 2208

PostPosted: Wed 09 Oct 2019, 16:25    Post subject:  busybox getopt ignores 1st positional paramater  

Some code:
Code:

  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:

  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:

+++ 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:

# 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 minds and on pearltrees.
Back to top
View user's profile Send private message Visit poster's website 
Burunduk

Joined: 21 Aug 2011
Posts: 76

PostPosted: Thu 10 Oct 2019, 14:02    Post subject:  

You are not the only one asking this question:

https://unix.stackexchange.com/questions/417454/util-linuxs-getopt-ignores-first-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:
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.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [2 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0485s ][ Queries: 11 (0.0062s) ][ GZIP on ]