Questions tagged [array]
A array is the simplest data-structure for storing items in continuously memory
641 questions
0
votes
0
answers
66
views
zsh associative array value data types
In ZSH, how do you set the value of an associative array parameter to an array?
e.g.,
% declare -A a=()
% a[first]=(1 2)
zsh: a: attempt to set slice of associative array
And when I try and append ...
1
vote
1
answer
84
views
What's the difference in manipulating the array between echo and printf in Bash?
I'm surprised by the difference in manipulating the array between echo and printf in Bash:
printf cannot handle += operator
Both printf and echo cannot get results of += out of while loop
So, why?
I'...
0
votes
3
answers
109
views
Making each word in a text file an item in a bash array
I have a string of text and spaces which looks like this:
macOS windows arch-linux ubuntu_linux
I want to append each item (with whitespace denoting a break between items) to a bash array. How do I ...
4
votes
1
answer
140
views
How to split output of a shell command on spaces while taking into account quoting?
I have a command that produces a list of arguments, quoting then if neccesary (https://docs.python.org/3/library/shlex.html#shlex.quote). I need them to pass them as command arguments in a zsh script (...
1
vote
4
answers
204
views
Need shell script help - processing the same option multiple times
So I've ran into a bit of a wall, I have an option in my script that calls a function which allows me to specify a file/directory and then I want to parse that output into a menu tool (using dmenu in ...
5
votes
4
answers
964
views
Pass multiple files as a single option
I'm trying to build a wrapper to execute a tool multiple times, then concatenate some of the results. I'd like to pass two sets of files to my wrapper script, then run the tool for each pair of files. ...
2
votes
2
answers
523
views
Zsh Expanding Variables into Arrays or Lists
I looked through some of the posted threads and none of them cover my query.
I have a simple line of code below which prints all the ASCII characters:
echo {' '..'~'}
I want to be able to use a ...
3
votes
3
answers
313
views
How to use associative array in Bash to get the current time in multiple locations?
I want to create a Bash script that outputs the following:
Chile: 03:46am, Friday, 27 September, 2024
Tierra Del Fuego: 03:46am, Friday, 27 September, 2024
Falkland Islands: 03:46am, Friday, 27 ...
3
votes
1
answer
199
views
Treat unset variables as an error when substituting (set -u) ignores array expansion
I don't understand why the array expression is fine.
$ set -eu
$ echo "${envs[*]}"
$ echo "${envs}"
bash: envs: unbound variable
How can I make bash fail also on such array ...
2
votes
3
answers
689
views
split and echo bash array with newlines to terminal
I create an array with a comma for delimiter:
echo "${myarray[*]}"
# prints: 22,3,2,0,22,4,5,8,22,4,3,6
I'd like to print it to terminal in chunks of four, with a newline and no comma ...
1
vote
1
answer
116
views
How to assign matching node values from xmlstarlet to an array in bash?
The loop below finds all of the nodes where the value equals "DOWNLOADING" and grabs the value of the corresponding "bytesdownloaded" node. I was expecting adlBytesDL1[0] to equal ...
7
votes
1
answer
282
views
Where are Perl arrays @0, @9999 documented?
$ perl -wle 'my @388=0..2;'
Can't use global @388 in "my" at -e line 1, near "my @388"
Execution of -e aborted due to compilation errors.
$ perl -wle '@388=0..2;'
$
Where can I ...
0
votes
3
answers
2k
views
How to add a prefix to every element of an array in shell
I am reading inputs from user in my script with spaces, and those multiple inputs I want to use in a command further down by adding -l in front of each one of those inputs. Is there a way to do this ...
3
votes
1
answer
3k
views
jq skip iteration over null
I am using jq 1.7
Data
I am operating on info.json downloaded by yt-dlp
yt-dlp --write-info-json --skip-download https://www.youtube.com/watch?v=vlIO-7Rpi7c
JQ - Task
Now I want to remove all array ...
0
votes
2
answers
309
views
Iterating arrays in TCL where array names have a numeric suffix
In TCL, I have a few arrays whose names have a numeric suffix
(i.e., whose names end with a number), like below:
array set ps0 [ list 0 15.885 1 55.43 1 0.254 2 0.227 3 0.177 ]
array set ps1 [ list 0 ...
4
votes
1
answer
682
views
Why does filtering a bash array using negative pattern matching and parameter expansion have unexpected results?
I'm doing a small-ish pure bash script to roll dice; as such I'm having to manipulate arrays. I want to do something akin to a filter operation in other languages: extract some of the things in the ...
4
votes
4
answers
577
views
How to sort the list of positional parameters in POSIX sh
Is there a way to sort the list of positional parameters in POSIX sh? Each positional parameter could contain arbitrary characters (e.g. spaces, newlines, tabs, etc.). The sort algorithm should be ...
1
vote
2
answers
4k
views
Bash manage space separated list
I have a rather complex shell script which processes several list of values which are read from a file as space separated values e.g.
SET1="value1 value2 value3"
for i in ${SET1}; do
...
...
1
vote
2
answers
360
views
Getting an array of files built from include array and exclude array containing globs
I am wanting to do the following:
Define an array of globs that specify a base collection of files to include in a process.
Define an array of globs that specify files to exclude from that process. ...
1
vote
1
answer
438
views
How to save the output of a sliced array as separate items rather than a string, when set as a var?
When slicing an array the output is captured in a variable as a string.
When the slice is used directly it will be separate items.
How can I save a sliced array in a variable without it being turned ...
2
votes
1
answer
582
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 ...
0
votes
2
answers
111
views
Read new lines from file and compare with two arrays if one or more word exists the result should not be written
Big fan of stackoverflow. Am a beginner myself and have found a lot of help on this site but have run into problems now.
Today I have a function like the following.
I read a text file (data.txt) for ...
-1
votes
1
answer
496
views
Zsh string variable containing multi words must be assigned to an array
How to have Zsh string variable containing multi words separated by space be assigned to an array so that each word is the array element
s='run help behind me'
a=($s)
m=${a[0]}
n=${a[1]}
print "m=...
2
votes
2
answers
643
views
For loop through a variable vector
I have a for loop and case statements. The for loop has quite a bit element list and the case statement will assign an 1D array or a vector. These values will be used in for loop after. I have the ...
0
votes
3
answers
304
views
In a bash script, loop through a bash array for input to a binary
I'm running bash.
Suppose I have a binary mybinary that takes interactive user input.
Suppose that mybinary takes attendee names as input until the user types q and presses Enter (q for "quit&...
17
votes
5
answers
24k
views
How can I create a multidimensional array, or something similar, with bash?
I need to create a multidimensional array with bash and I read that there is no such thing as multidimensional arrays in bash .
Here are my possible data, what they look like and what I need. This is ...
0
votes
1
answer
138
views
Bash create parameter named array within function
I'm attempting to write a function that writes arrays with a name that's passed in. Given the following bash function:
function writeToArray {
local name="$1"
echo "$name"
...
0
votes
2
answers
63
views
Special array from list of items in current dir
I want to extract a specially formatted array from the list of current dir.
$ ls
Output:
fileABC.ext1
filePQR.ext2
dirMNO
dirFGH
I want only the dir and files of specific extension
I want an array ...
0
votes
1
answer
970
views
In bash shell, how can I filter an array of paths based on whether an element exists?
I'm using bash shell. I have this array representing file paths
MY_FILES=(/path/to/file1 /tmp/abc.txt /tmp/test.sh /path/to/file2 /path/to/file3 /tmp/abc.txt)
How can I form a new array of elements ...
0
votes
2
answers
496
views
Sort file array list in bash by date and bypass argument limit
So, I have a file array list in a bash shell, and I want to sort all the files in the array by the date modified, starting with the oldest being the first one in the array. However, I don't want to ...
1
vote
1
answer
346
views
Completed stumped by trying to use a decimal value in an array for Bash
Here is some simple test code.
#!bin/bash
cpm=(0 1 0.094)
lv=1
attack=5
defense=9
stamina=16
echo $((cpm[lv]))
mycpm=$((cpm[lv]))
#mycpm=`echo "0.094" | bc -l`
cq=`echo "$attack*$...
0
votes
2
answers
1k
views
Curl exit immediately if no internet
I want to fetch some apis from github.
so this is the code
array=(link1 link2 link3 link4)
readarray -t item < <(for i in ${array[@]}
do curl -s "$i" | jq '.tag_name'
done)
...
0
votes
1
answer
365
views
bash script - printing a value of an array based on the value of another array
I have two arrays and want to print a value from ARRAY2 depending on the applicable value in ARRAY1.
#!/usr/bin/env bash
ARRAY1=(bb.service.sql bw.service.sql)
ARRAY2=(bb bw)
case $ARRAY1[@] in
${...
1
vote
1
answer
11k
views
jq: error (at input.json:45): Cannot index array with string "value"
I try to convert my .json files to .csv with jq. I'm getting cannot index array with string value on all my values. .json
{
"organic_data": [
{
"description": "...
4
votes
2
answers
492
views
Is it possible to apply substring expansion to all elements of a zsh array?
Let's say I'd like to remove the first two and last three characters from all elements of an array e.g.
results=( QK9H9UtADCgnG AlaLkCADjQ krsxseW8H1VrU 6nBG94ZbCWQ )
I'd like to end up with
results=(...
0
votes
1
answer
832
views
How to replace matching pattern in an array
I have array with name 'arr' which has the following value -
post-ro
post-ro-1
post-ro-2
I have this variable:
str="su scp [email protected]:/etc/bp/pg/pri/user.json /etc/bp/pg/pri"
I want to ...
0
votes
2
answers
157
views
Loop over space delimited PIDs, get number of open file handles
How may I loop over a set of PIDs delimited by spaces? I think I remember the first step is like making each element its own row?
For my Firefox sessions they are quite a few PIDs:
$ pidof firefox
...
0
votes
1
answer
71
views
linux bash shell script - pass through function array variable
So in order to be versatile, I make a lot of functions and i want to define my own variable name when i call on that function.
For basic text, numbers it works and this is how I do it
function ...
2
votes
1
answer
579
views
Capture output of a command that get stuck
I am trying to use regex for capturing some text pattern in an ouput of a command and create an array list_mqs. The problem is that this previous command get stuck and never terminates. So, when I use ...
0
votes
1
answer
166
views
Can't remove element from the array
Does anyone can explain me why the pid is not deleted from my pids list ?
The for loop still occure the same number of time each time I go in this
The var i is equal of the number of pids. So the loop ...
4
votes
1
answer
6k
views
Convert comma separated string into json array
I have the following string in bash
"3.8,3.9,3.10"
Is there a way using shell to convert it into a json array, i.e.
["3.8", "3.9", "3.10"]
0
votes
2
answers
596
views
copy multiple directories into one folder from a variable or loop array in bash?
I am trying to target 100+ directories to perform a copy. The whole script needs to be contained within one file; therefore, I can't have the directories saved in a separate file. I want to be able to ...
3
votes
1
answer
939
views
Is it possible to pass a bash array as a parameter to a function? [duplicate]
I'd like to pass an array to a bash function but I get a bad substitution error
Example
mapfile -t ray < <(parallel -j 0 echo ::: {1..10})
declare -p ray
declare -a ray=([0]="2" [1]=...
1
vote
1
answer
3k
views
Ansible: set_fact after successful variable match in array
I have following playbook ~ # cat demo.yml:
- name: demo
hosts: localhost
gather_facts: no
vars:
set:
task:
type: var1
task:
- type: var1
- type: var2
- type: ...
0
votes
1
answer
491
views
How to declare array from STDOUT in Buildspec, Codebuild, AWS
I'm having a problem with declaring array from STDOUT in Buildspec, which is part of Codebuild in AWS.
Buildspec using Pure Bourne Shell, so I can't use this line in code declare -a available_envs=($(...
0
votes
1
answer
110
views
how to combine output values using echo & fsck
I am trying to make a functional script just using Partition Label, get that label, from the Label get the Device, and pass fsck -fy twice on each partition.
#!/usr/bin/env bash
# Filter partition by ...
-1
votes
2
answers
500
views
How do you count the number of tokens from two columns of words from a csv file inside a loop?
I have a comma-separated csv file with 50 lines. One column is for state names and the other column is for capitals (of the states). How do you make a loop where it counts the number of tokens (2, 3, ...
0
votes
1
answer
284
views
How to make Zsh interpret \n in parameter expansion
I’m using Zsh’s parameter expansion flags to split a string into an array.
This works:
string="one
two"
array=("${(f)string}")
echo "${array[1]}"
# Returns:
# one
This ...
1
vote
1
answer
1k
views
Create an array from the output of whiptail command
So, I am trying to use whiptail command to give users option to choose different things they want to install in their system.
I use the whiptail --checklist command as below:
name =$(whiptail --title &...
1
vote
1
answer
750
views
Can I import a single row (indexed) from a csv file into a zsh array?
I want to parse a csv file in zsh row by row and store it in an array (without the commas). Is it possible to import a single row into an array in zsh, then afterwards grab the next row?
The issue is ...