Questions tagged [associative-array]
The associative-array tag has no summary.
65 questions
7
votes
1
answer
362
views
Cannot delete bash associative array element
Consider an associative array in bash (versions 5.2.15(1)-release and 5.2.21(1)-release):
declare -A ufs=(); ufs["one"]=1; ufs["two"]=2; ufs["three"]=3
printf '> %s\n' ...
-2
votes
1
answer
113
views
Associative Array contains filenames and paths
I have a pipeline that deploys scripts into a temporary dir. I need to rellocate them into their actual proper directory.
#!/usr/bin/sh
DEPLOY_HOME=/opt/xxx
function getDeployedFiles {
ls ${...
2
votes
1
answer
581
views
What is difference between these two declarations of associative arrays in Bash?
I am playing a bit with associative arrays in Bash and I found the following difference when declaring the exact same associative array with and without declare. The code is as follows:
#!/usr/bin/env ...
3
votes
1
answer
346
views
duplicated entries of an array in awk
I have a file with 4 columns. When I put these 4 columns into an array using NR as the index, the entries are duplicated somehow. See below for an elaboration of the issue.
The first 5 lines of the ...
1
vote
4
answers
4k
views
bash loop associative array with variable array name
I have a lot of associative arrays and i want to use only 1 loop. select the array for loop by a given name
I want to select/build a part of the arrayname with a variable and than loop with that name, ...
8
votes
2
answers
3k
views
How to set environment variables dynamically for command?
I would like to use some kind of minimalistic template engine with pure bash and envsubst:
user@host:~$ env -i FOO=foo BAR="bar baz" envsubst '$FOO,$BAR' \
<<< 'Hello "$FOO&...
2
votes
3
answers
2k
views
How can I create a JSON object from an associative array in shell using jo?
I know about How to create JSON from associative array but that's not my problem.
I have this associative array:
declare -A aliases
aliases[Index]=components/Index/Exports
aliases[Shared]=components/...
3
votes
1
answer
134
views
How can i use a parameter as an inner array-name to my script?
When i write a script named array_call_self.sh as follows
#!/bin/bash
declare -A num word
word=(
[a]='index_a'
[b]='index_b'
[c]='index_c'
)
num=(
[a]=1
[b]=2
[c]=3
)
array=${$1[@]}
for i in ${$...
2
votes
2
answers
1k
views
Bash pass an associative array to/from a background function
I am trying to pass a bash associative array by reference into a function and then be able to see the changed content back in the main script after the function is complete. I have found what seems to ...
0
votes
0
answers
555
views
Associative array in shell script
I am trying to map a hostname to a queue manager name using the following array. I get an error with bad array script. What am I doing wrong here
declare -A managers
while read -r mgr host; do
...
1
vote
1
answer
2k
views
How to retrieve items from an array of arrays?
Hello StackExchange pros!
I am working on a zsh project for macOS. I used typeset to create three associative arrays to hold values, and a fourth array to reference the individual arrays. Is it ...
6
votes
2
answers
7k
views
Bash: value too great for base when using a date as array key
I have read about specifying "10#", but I don't think it's my case as I am not doing number comparison. I am trying to create an associative array in Bash, and the code worked fine until ...
10
votes
1
answer
2k
views
In ZSH, how do I unset an arbitrary associative array element?
An associative array I have has arbitrary keys, including keys containing backquotes, brackets, etc:
$ typeset -A arr
$ key='`'
$ arr[$key]=backquote
$ echo $arr[$key]
backquote
I now need to unset ...
0
votes
0
answers
236
views
Unexpected behavior during index assignment in a Bash array [duplicate]
I'm having trouble assigning values to a specific bash index,
but apparently only when the index variable is set using a while read loop.
Taking this code as a test example:
#!/bin/bash
read -d '' ...
4
votes
4
answers
2k
views
zsh testing existence of a key in an associative array via indirect expansion
So I know that you can test for the existence of a regular parameter via indirect expansion by doing something like:
foo=1
bar=foo
(( ${(P)+bar} )) && print "$bar exists"
And I know you can ...
1
vote
0
answers
2k
views
How to make a simple menu in bash from an associative array
I am refactoring a script that displays a text menu similar to this:
Select a mounted BTRFS device on your local machine to backup to.
1) 123456789abc-def012345-6789abcdef012 (/)
2) 123456789abc-...
2
votes
2
answers
370
views
I am having difficulties with back-reference in awk
Recently, I am into security logs and want to make it better way on bash-shell. I found out in awk back-references are only stored by 9.
But I need to use 10 back-references.
Tried
awk '{print ...
2
votes
1
answer
7k
views
how to combine 2 arrays into one associative array
i need to combine ARRAY1 and ARRAY2 into an associative array like ARRAY. i'm using this code:
mapfile -t ARRAY1 < <(/bin/awk '{ print $ 1 }' /output/gen_branch)
mapfile -t ARRAY2 < <(...
0
votes
0
answers
20
views
associative array execution order problem [duplicate]
in a bash script i'm using an associative array to create tables, tablespaces, indexes and partitions in my database. this is the script (the main script is too long with complicated queries so i ...
0
votes
1
answer
2k
views
updating one file based on values in another with AWK
I have two files file1.csv (20 columns 410k rows) and data.csv (4 columns 1800 rows). What I am trying to do is if data.csv 1st column matches file1.csv 2nd column overwrite 1st column in file1.csv ...
2
votes
2
answers
2k
views
no matches found when using associative arrays in zsh
I am encountering no matches found when using map in zsh:
#!/bin/zsh
declare -A map=(["8761"]="Eureka服务" ["11001"]="用户微服务")
Why would this happen, and how can I fix it? This is the error:
~/source/...
0
votes
1
answer
55
views
calling associative arrays [closed]
Team, am setting some variables in an associative array but its output is not resulting anything.. any hint?>
#/bin/bash
#IOEngine="psync"
#TestType="read"
IOEngine="libaio"
TestType="randread"
...
10
votes
4
answers
2k
views
Inverting an associative array
Let's say I have an associative array in bash,
declare -A hash
hash=(
["foo"]=aa
["bar"]=bb
["baz"]=aa
["quux"]=bb
["wibble"]=cc
["wobble"]=aa
)
where both keys and values are ...
6
votes
3
answers
12k
views
linux bash dictionary check if empty
How to check if a dictionary (associative array) is empty? I just declare one using declare -A dict. I want to know if it is just declared but not have any key.
7
votes
1
answer
11k
views
Bash return an associative array from a function and then pass that associative array to other functions [closed]
I'm trying to build an associative array in a function from a list pass in via an arg, but It just isn't working:
#!/usr/bin/env bash
function cwd {
echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" &...