Menu

#45 set args is flawed when one parameter is an array

v1.0_(example)
closed
5
2016-12-14
2016-12-12
No

Considering the following script which accepts 2 parameters:
+ files_list which is expected to be an array
+ command which is expected to be a string

1
2
3
4
5
#!/bin/bash
shopt -s extglob

files_list=$1
command=$2

1) First method: passing the array when calling bashdb

# bashdb /usr/bin/.drive-format-driveinclude -- files_to_push pushbash debugger, bashdb, release 4.4-0.92

Copyright 2002, 2003, 2004, 2006-2012, 2014, 2016 Rocky Bernstein
This is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.

(/usr/bin/.drive-format-driveinclude:55):
55: shopt -s extglob
bashdb<0> s
(/usr/bin/.drive-format-driveinclude:59):
59: files_list=$1
bashdb<1> s
(/usr/bin/.drive-format-driveinclude:60):
60: command=$2
bashdb<2> s
(/usr/bin/.drive-format-driveinclude:62):
62: section='out'
bashdb<3> x files_list
declare -- files_list="--"
bashdb<4> x $command
files_to_push
bashdb<5> 

As we can see here, both parameters are wrongly set.

2) Second method: setting the array inside bashdb with set args

# bashdb /usr/bin/.drive-format-driveinclude 
bash debugger, bashdb, release 4.4-0.92

Copyright 2002, 2003, 2004, 2006-2012, 2014, 2016 Rocky Bernstein
This is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.

(/usr/bin/.drive-format-driveinclude:55):
55: shopt -s extglob
bashdb<0> set autoeval on
Evaluate unrecognized commands is on.
bashdb<1> files_to_push=([0]="CVs/Consultant Réseaux.odt" [1]="CVs/Consultant Réseaux.pdf" [2]="CVs/Ingénieur Réseaux.odt" [3]="CVs/Ingénieur Réseaux.pdf" [4]="CVs/Network & Security Engineer.odt" [5]="CVs/Network & Security Engineer.pdf")
$? is 0
bashdb<2> x files_to_push
declare -a files_to_push=([0]="CVs/Consultant Réseaux.odt" [1]="CVs/Consultant Réseaux.pdf" [2]="CVs/Ingénieur Réseaux.odt" [3]="CVs/Ingénieur Réseaux.pdf" [4]="CVs/Network & Security Engineer.odt" [5]="CVs/Network & Security Engineer.pdf")
bashdb<3> seta args /usr/bin/.drive-format-driveinclude files_to_push push
/tmp/bashdb_eval_32703: line 2: seta: command not found
$? is 127
bashdb<4> s
(/usr/bin/.drive-format-driveinclude:59):
59: files_list=$1
bashdb<5> s
(/usr/bin/.drive-format-driveinclude:60):
60: command=$2
bashdb<6> s
(/usr/bin/.drive-format-driveinclude:62):
62: section='out'
bashdb<7> x files_list
declare -- files_list=""
bashdb<8> x $command

bashdb<9> 

As we can see again here, both parameters are wrongly set.

One workaround is to set the parameters directly after the first lines with:

bashdb<9> files_list=([0]="CVs/Consultant Réseaux.odt" [1]="CVs/Consultant Réseaux.pdf" [2]="CVs/Ingénieur Réseaux.odt" [3]="CVs/Ingénieur Réseaux.pdf" [4]="CVs/Network & Security Engineer.odt" [5]="CVs/Network & Security Engineer.pdf")
$? is 0
bashdb<10> command=push
$? is 0
bashdb<11> 

Discussion

  • Jean-christophe Manciot

    It does not appear in the post above, but "files_to_push" is of course set before calling bashdb in the first method:

    # files_to_push=([0]="CVs/Consultant Réseaux.odt" [1]="CVs/Consultant Réseaux.pdf" [2]="CVs/Ingénieur Réseaux.odt" [3]="CVs/Ingénieur Réseaux.pdf" [4]="CVs/Network & Security Engineer.odt" [5]="CVs/Network & Security Engineer.pdf")
    
     
  • Rocky Bernstein

    Rocky Bernstein - 2016-12-12

    I don't see what this has to do with arrays. I think bashdb is just confused by the '--' which is getting set as $1. The -- should be detected and removed.

    So a workaround for now is to not use -- in those cases where it is not needed. It is needed when options to the program might be confused with options to bashdb

    Again since it will take me a while to have time to fix this, feel free to use this information to work up a patch.

     
  • Jean-christophe Manciot

    As a matter of fact, my script is not correct: I over-evaluated bash simplicity to pass arguments.

    The right way to pass an array parameter to a script is as follows:

    declare -a **files_list=("$@")**
    

    So, now everything is fine when used with bashdb (without "--"):

    bashdb /usr/bin/.drive-format-driveinclude **"${files_to_push[@]}"** push
    
    bash debugger, bashdb, release 4.4-0.92
    ...
    (/usr/bin/.drive-format-driveinclude:55):
    55: shopt -s extglob
    bashdb<0> set autoeval on
    Evaluate unrecognized commands is on.
    bashdb<1> s
    (/usr/bin/.drive-format-driveinclude:59):
    59: declare -a files_list=("$@")
    bashdb<2> s
    (/usr/bin/.drive-format-driveinclude:60):
    60: command=$2
    bashdb<3> s
    (/usr/bin/.drive-format-driveinclude:62):
    62: section='out'
    bashdb<4> x files_list
    declare -a files_list=([0]="CVs/Consultant Réseaux.odt" [1]="CVs/Consultant Réseaux.pdf" [2]="CVs/Ingénieur Réseaux.odt" [3]="CVs/Ingénieur Réseaux.pdf" [4]="CVs/Network & Security Engineer.odt" [5]="CVs/Network & Security Engineer.pdf" [6]="push")
    bashdb<5> 
    

    You can mask this post as "Not a bug"

     
  • Rocky Bernstein

    Rocky Bernstein - 2016-12-14
    • status: open --> closed
    • assigned_to: Rocky Bernstein
     
  • Rocky Bernstein

    Rocky Bernstein - 2016-12-14

    93d8abd addresses the bug where bashdb was getting confused by -- in setting arguments.

     

Log in to post a comment.