Questions tagged [array]
A array is the simplest data-structure for storing items in continuously memory
625
questions
0
votes
2
answers
148
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
...
...
0
votes
2
answers
62
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
229
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
496
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
38
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
53
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
339
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
169
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&...
8
votes
4
answers
4k
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
53
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
40
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
318
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
222
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
102
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
268
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
98
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
${...
0
votes
1
answer
2k
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
171
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
254
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
70
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
45
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
335
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
72
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 ...
2
votes
1
answer
3k
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
330
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
540
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
2k
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
270
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
81
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
319
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
119
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
468
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
356
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 ...
5
votes
3
answers
1k
views
Case on multiple variables at once bash
I want to know if there is any way to case on multiple variables at once, like this:
#/bin/bash
arr1=(1 2)
arr2=(3 4)
foo=1
bar=2
case $foo && $bar in
${arr1[@]})
echo "...
1
vote
2
answers
124
views
What's the upper-bound for an array index/subscript in GNU bc?
I've found that there is an upper-bound on your subscripts/indices in an array in GNU bc. Running interactively and asking to let arr[100000000]=42 returns an error:
Runtime error (func=(main), adr=...
1
vote
1
answer
2k
views
Call a command from a shell script, passing most arguments, allowing arguments with blanks
I am have a wrapper run_sas.sh around a command sas that runs SAS code in batch. A typical call looks like this
./run_sas.sh -sysin /my_code/my_program.sas -log /my_log_folder/my_program.log
run_sas....
1
vote
1
answer
156
views
Assigning an array value in zsh for gsettings keybindings automation
I think this script should work to set keybinds, but it's currently quite borked--UPDATE: it has been unborked, and now works. It is ugly though, and I'm soliciting ways to make it more elegant, as I ...
0
votes
2
answers
69
views
get value of variable which is prefixed by another variable in bash
I'm running offlineimap on a bunch of accounts and want to check the exit code of each run and perform some actions accordingly.
I have 6 separate email accounts that I run against with a lot of code ...
1
vote
1
answer
902
views
How to append to an array with a dynamic variable name?
section_example=(one two three)
name=example; section_$name+=(four)
bash: syntax error near unexpected token `four'
Section names are unknown beforehand. eval and declare -a output the same error. ...
8
votes
2
answers
3k
views
In bash, how do I get the index of the last element of an array without a loop
In bash, is it possible to get the index of the last element of an array (that might be sparse) without looping through the entire array like so:
a=( e0 e1 ... )
i=0
while [ "$i" -lt $(( ${#...
3
votes
1
answer
698
views
Cut or Awk to save lines on array in bash
I have a scenario that I have to put lines of a file .txt inside an array, example of file content;
type of host
|
| first host
| |
| | second host
| | |
| | | same ip ...
1
vote
1
answer
3k
views
Ansible convert dictionary to mix of dictionary/array
I use following files:
variable file: db.yml
x86_64:
alpine:
version: 3.15.0
debian:
version: 11.3.0
aarch64:
alpine:
version: 3.15.0
debian:
version: 11.3.0
playbook: ...
0
votes
1
answer
729
views
convert a uint8 array ( javascript new Uint8Array() ) to binary format (or binary mode ?) in shell / bash?
The question is about convert from new Uint8Array() generated from javascript into a normal file (file that can be used with other programs like ffmpeg)
** I don't know the right term to use if it's ...
0
votes
1
answer
443
views
Counting file extensions in a string array
I have a $FILES array which consists of strings of 4 filenames:
FILES=$(ls /my/directory | shuf -n 4)
The contents of the array look something like: file1.jpg file2.jpg file3.gif file4.mp4
I want to ...
2
votes
5
answers
526
views
match value2 in 2 files if value 1 is exact match
I have 2 files containing list. Column 1 is userIds & column 2 is associated values
# cat file1
e3001 75
n5244 30
w1453 500
#cat file2
d1128 30
w1453 515
n5244 30
e3001 55
Things to consider.
...
5
votes
2
answers
2k
views
how to test if index exists in array
I'm writing a Git Bash utility that copies a project folder from one location to another. There are multiple destinations to which the user may want to copy the project, though only one location per ...
3
votes
1
answer
4k
views
Trying to put output of grep command into a bash array
If I run the following grep command I get the output I am expecting:
$ grep -o -E '/ |:|\\|<|>|\*|\?|\"| $' <<< '/home/ttwaro/temp/Test/This is a Test: Testing Number 1?/This is ...
1
vote
2
answers
275
views
awk matching fields in 2 separate files using command containing {BEGIN FS = "|"} returns output of blank lines
Two files: data1
Name |formula |no. |dose|days|cost |msg|em|notes
Fname-Lname|BXXXT+GG |8262|4 |14 |57.78 | | |sq
Fname-Lname|SJXXT+GG |8263|4¾ |...
1
vote
0
answers
955
views
Create a tsv file with data from several files
How can I create a tsv file from multiple files?
I want to be able to create a tsv file from several different files. One sample is associated with five 'region' files that have data in them. For ...
1
vote
1
answer
4k
views
Getting the first/last/nth result from a jq select result
I am trying to find a way to pipe the result of my current script into another command which will leave me with just the last result of the array I get in my select here:
jq -r --arg name "$1&...