Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug for associative arrays when using gosh with -c vs without #884

Closed
everactivetim opened this issue Jun 24, 2022 · 2 comments · Fixed by #893
Closed

Bug for associative arrays when using gosh with -c vs without #884

everactivetim opened this issue Jun 24, 2022 · 2 comments · Fixed by #893

Comments

@everactivetim
Copy link

I found this because task uses this and that's where it cropped up. So I came here and started trying to debug it and found gosh to use as a potential quick way to investigate.

It seems that when run without -c it iterates over each statement and executes them separately but when using -c it treats the whole file as a script. I guess there's something about context there that's being lost?

Anyway, consider this script:

#!/bin/bash

declare -A database_lookup=()
database_lookup[management]=management
database_lookup[devicetwin]=devicetwin
database_lookup[identity]=identity

for d in "${!database_lookup[@]}"
do
    echo "$d -- ${database_lookup[$d]}"
done

The output will be quite different depending on whether you use -c with gosh or not. This exactly models the behavior I am seeing in task.

Is this a bug or am I (and the task project by extension) doing it wrong? Thanks!

@mvdan
Copy link
Owner

mvdan commented Jun 28, 2022

Both seem to produce the same output on 5146d3e:

$ cat input
#!/bin/bash

declare -A database_lookup=()
database_lookup[management]=management
database_lookup[devicetwin]=devicetwin
database_lookup[identity]=identity

for d in "${!database_lookup[@]}"
do
    echo "$d -- ${database_lookup[$d]}"
done
$ gosh <input
devicetwin identity management -- 
$ gosh -c "$(<input)"
devicetwin identity management -- 

That said, the result looks wrong. Bash gives:

$ bash <input
identity -- identity
devicetwin -- devicetwin
management -- management

I assume that's the bug you refer to - looking.

@mvdan
Copy link
Owner

mvdan commented Jun 28, 2022

Shorter repro:

$ cat input
declare -A arr=([a]=x [b]=y)
for k in "${!arr[@]}"; do
	echo "$k: ${arr[$k]}"
done
$ bash <input; echo ===; gosh <input
b: y
a: x
===
a b: 

mvdan added a commit that referenced this issue Jun 29, 2022
That is, add support for "${array[@]}" for associative arrays,
as well as "{!array[@]}" for indexed and associative arrays.

We also add support for positional parameters in indirections,
so that ${!@}, ${!*}, and ${!1} work the same as Bash.

As usual, with plenty of tests.

Fixes #884.
@mvdan mvdan closed this as completed in #893 Jul 3, 2022
mvdan added a commit that referenced this issue Jul 3, 2022
That is, add support for "${array[@]}" for associative arrays,
as well as "{!array[@]}" for indexed and associative arrays.

We also add support for positional parameters in indirections,
so that ${!@}, ${!*}, and ${!1} work the same as Bash.

As usual, with plenty of tests.

Fixes #884.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants