Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Join us in building a kind, collaborative learning community via our updated Code of Conduct.

Questions tagged [array]

A array is the simplest data-structure for storing items in continuously memory

0
votes
1answer
14 views

command not found assigning value to array in bash

I'm trying to assign a value if the array at a certain index is empty, but I'm getting a command not found error. while IFS=$';' read -r -a array do # if empty set Not Available if [[ -z ${array[6]} ...
2
votes
2answers
34 views

jq - add objects from file into json array

I want to add an array with elements and value into an existing json file using jq. I already have a file (input.json) with { "id": 9, "version": 0, "lastUpdTs": 1532371267968, "name": "...
3
votes
1answer
71 views

split zsh array from subshell by linebreak

I would like to instantiate a zsh array from a subshell myarray=($(somecommand) and I check if I received what I wanted with for element in $myarray ; do echo "===" ; echo $element ; echo "---" ; ...
0
votes
2answers
32 views

Bash : Give builded array to function as a list of argument?

I have this issue with borgbackup, but because the reaction is the same, I will use rsync in my example. I want to build an array of arguments by adding a prefix to each, and then give that array to ...
2
votes
7answers
84 views

Create PHP array from letters

I would like to create a PHP array from the Serbian alphabet. Out of а б в г д ђ е ж з и ј к л љ м н њ о п р с т ћ у ф х ц ч џ ш a b c č ć d dž đ e f g h i j k l lj m n nj o p r s š t u v z ž ...
2
votes
2answers
68 views

Index range of array doesn't allow you to iterate over a new line in bash, please help!

I'm working on a simple bash script that fixes a duplicate naming issue on a retropie. The script itself grabs any name that is mentioned more than once in a gameslist.xml file, then stores those in ...
0
votes
1answer
49 views

How to set empty array elements set to zero?

I'm writing a script that sends the result of commands to an output array. It involves checking servers for logs and retrieving their sizes, however there are cases in which the server has a failover ...
2
votes
1answer
34 views

Detect optional function argument (array)

Consider this function: function add_one(in_ar, each) { for (each in in_ar) { in_ar[each]++ } } I would like to modify it such that if a second array is provided, it would be used instead ...
0
votes
1answer
46 views

Generate a list of IPv6 into an array

I wrote this script a little while ago : #!/bin/bash #Default number of IPs NUM_IPS=48 if [ "$1" != "" ]; then NUM_IPS=$1 fi #Example of IPv6 supplied 2001:41d0:0303:6e1b::/64 read -p "...
2
votes
2answers
58 views

Why doesn't the separator works for array expansion?

I want to limit the change to the separator only to the following echo command not to the shell: $ myarr=(1 2 3) $ echo $( IFS="|"; echo "${myarr[@]}" ) 1 2 3 $ echo $( ( IFS="|"; echo "${myarr[@]}" )...
5
votes
3answers
999 views

How do I capture a MySQL result set in a bash array?

I'm using bash shell on CentOS 7. I want to run a MySQL query from a shell script and iterate over each row of results. If there were 4 rows returned, I thought I could capture the four rows in an ...
2
votes
2answers
49 views

Zsh, Indirect array variable assignment without using eval

I have a variable VARNAME which contains a name of another variable. I'd like to assign to this another variable without using eval. How can I do that? Th reason I don't want to use eval is the ...
6
votes
1answer
211 views

Unexpected outcome of a=“$@”

I'm struggling with this situation: $ set -- 1 2 3 $ a="$@" $ echo "$a" 1 2 3 What I find unexpected is the assignment itself. man bash says this about the "$@" expansion: When the expansion ...
2
votes
5answers
402 views

Number of elements/words in a shell array variable

I have looked at the question How to count number of words from String using shell on SO, which explains how to count words inside a variable. But this only counts one word inside my variable so I ...
2
votes
1answer
105 views

Why does this code work in bash v4.4 but not bash v3.2?

I have the following bash script: #!/bin/bash encl0=( 0,0 0,1 0,2 0,3 0,4 0,5 0,7 0,8 0,9 0,10 0,11 0,12 0,13 0,14 0,15 ) MISSING_DISKS=() OLDIFS=$IFS IFS=$'\n' MISSING_DISKS+=($({ printf '0 %s\n' {...
0
votes
1answer
51 views

bash script to find files thinks the file name should be an integer

I am writing a script to obtain a list of file names from a text file, so that it can find the files and copy them to a specific folder. When I run the script, I get the following error: ./...
0
votes
1answer
36 views

bash script load an modify array from external file

I have written a script that is reading an array: recentdirs with directory names from an external config file called: defaults.cfg and the user can either chose one of the directories from the list ...
1
vote
2answers
137 views

How to join elements of an array in POSIX delimited by a space?

The following works in a Bash script: PACKAGES=( 'curl' 'git' 'htop' 'mc' 'tree' 'vim' ) apt --yes install ${PACKAGES[@]} But how can I do the same in POSIX so that I can ...
1
vote
1answer
43 views

Compact way to get tab-separated fields into variables

In Bash, I'm reading out fields from a line into an array. Let me stress that performance is an issue, so I can't afford anything that spawns subprocesses. To make the code more readable, I want the ...
0
votes
2answers
51 views

What exactly is the Length of an Array in bash and how to differ '*' and '@'?

I have the following example script and want to know what exactly is the length of an array, are this bytes, characters or what else? #!/bin/bash # Arrays # @ vs. * ape=( "Apple Banana" "Emacs ...
0
votes
1answer
222 views

Concatenating a list of files using For loop

I'm new to unix/bash/code in general, and I'm trying to merge multiple sample files with the same name in different directories into a new file in its own directory (the outputs of multiple data ...
3
votes
1answer
177 views

How to create a possibly empty array from filename glob?

With zsh, it is not difficult to create a possibly empty array from the results of a (possibly null) filename glob. For example: % pathlist=( /no/such/path/*(N) ); printf -- "%d\n" $#pathlist 0 ...
1
vote
1answer
92 views

How to add values to an array which contains a variable in the array name in bash?

I have an array in my script that takes a list of group names. This list does not have a fixed number of elements. GROUPS=(group1 group2 group3) Each element in this set of GROUPS can have an ...
3
votes
1answer
160 views

Is 'array=X' always assigned to first element in bash?

Is 'array=X' always assigned to first element in bash? If that is not,maybe I need some "for example" to understand following text from man bash page: When assigning to indexed arrays, if the ...
0
votes
1answer
49 views

Set two or more files for execution (via source) in a comfortable way to read

I used the following way to execute a collection of Bash script files, in the current Bash session: source ~/myScripts/{assignments.sh,nginx_conf.sh,php_conf.sh,drush_install.sh} It feels to me ...
4
votes
2answers
697 views

How to use pseudo-arrays in POSIX shell script?

I can't get my head on: How to use pseudo-arrays in POSIX shell script? I want to replace an array of 10 integers in a Bash script with something similar into POSIX shell script. I managed to come ...
0
votes
2answers
2k views

Ansible array variable

I have a template file config.j2. {% for host in groups['dbs'] %} ips= {{ hostvars[host].ansible_default_ipv4.address }} {% endfor %} My output is: ips= 192.168.231.91 ips= 192.168.231.92 ...
0
votes
1answer
13 views

RHSA installation in RH7

this is a question similar to the mine I asked you yesterday (how to iterate installation in a row). I need a script that starting from an RHSA list as argument, separated by " " spaces, it performs ...
3
votes
1answer
220 views

Bash: space as a key in an associative array

This code in Bash s="There are seven pencil" declare -A A while IFS= read -rn1 a; do [ -z "$a" ] || [ -n "${A[$a]}" ] && continue printf %s "$a" ((A[$a]++)) # A[$a]=x done <<<"$...
-1
votes
1answer
80 views

Sum of the array is not printing

I am trying to create a script where the sum of the array is what prints but my results just end up showing me "Sum: 0" Here is my script so far. Any help in correcting this would be appreciated. #!...
3
votes
1answer
74 views

Disadvantages of assigning an array to a variable like this: array2=(“${array1[@]}”)

I can copy an array to another like this: array2=("${array1[@]}") One of the disadvantages of this approach is that the indices will not be preserved. For example: if array1 have three elements with ...
0
votes
2answers
23 views

Invalid package name due to Bash array dereference [duplicate]

I'm trying to build Emacs from sources. When configure options were listed without an array Emacs configured properly. When I added a Bash array to add optional options it broke configure. Here is the ...
0
votes
1answer
620 views

linux + how to convert variable to array

we want to set variable that includes words as array folder_mount_point_list="sdb sdc sdd sde sdf sdg" ARRAY=( $folder_mount_point_list ) but when we want to print the first array value we get all ...
4
votes
4answers
1k views

how to shift array value in bash

we want to build 6 mount point folders as example /data/sdb /data/sdc /data/sdd /data/sde /data/sdf /data/sdg so we wrote this simple bash script using array folder_mount_point_list="sdb sdc sdd ...
1
vote
0answers
34 views

bash + how to set variable instead array element [duplicate]

we can build array list of a..z range as the following example1 list=({a..z}) echo $list echo ${list[*]} a b c d e f g h i j k l m n o p q r s t u v w x y z but in case we want to set variable in ...
1
vote
1answer
60 views

Better way to retrieve n-th (index) positional parameter value in `ash` then `eval echo \$$n`?

I'd wish the user to select a file from a numbered list on screen in a mkinitcpio busybox ash environment. The list needs to be filled/sorted from top to bottom and printed in columns, like: 1) 2017-...
0
votes
1answer
127 views

Since which version does busybox ash built with ASH_BASH_COMPAT include array support?

March 12th, 2009 Denys Vlasenko wrote: Plans to implement it — BASH like array support — eventually for ASH_BASH_COMPAT, yes. Need to have enough free time for that. (volunteers ...
13
votes
7answers
2k views

How to test if array elements are all equal in bash?

The following array represented the numbers of disks on each linux machines Each single array includes the number of disks on a linux machine. echo ${ARRAY_DISK_Quantity[*]} 4 4 4 4 2 4 4 4 what is ...
10
votes
8answers
3k views

Bash - reverse an array

Is there a simple way to reverse an array? #!/bin/bash array=(1 2 3 4 5 6 7) echo "${array[@]}" so I would get: 7 6 5 4 3 2 1 instead of: 1 2 3 4 5 6 7
2
votes
2answers
153 views

bash - re-sort array as if it was a circle by setting a startpoint

I use this kind of setting in a script: #!/bin/bash array=(C G D A E B) if [[ "$1" = "--setstart" ]] || [[ "$1" = "-s" ]]; then if [ -n "$2" ]; then array=($(sed "s/.*$2 /$2 /" <<...
22
votes
3answers
2k views

Possible bug in Bash?: foo() { echo “${var[0]}”; }; var=(bar baz) foo

OS: Ubuntu 16.04.3 Shell: Bash 4.3.48 I know that is possible to temporarily change the content of a variable as in var=value command, being probably IFS= read -r var the most notable case of this. ...
2
votes
3answers
2k views

Using case and arrays together in bash

Is it possible to check if a variable is contained inside an array using case? I would like to do something like ARR=( opt1 opt2 opt3 ); case $1 in $ARR) echo "Option is contained in the ...
1
vote
1answer
3k views

splitting a line into array in bash with tab as delimiter

I have a file in the following format and it is tab separated a k testis adult male 8 week rRNA b k testis adult male 8 week rRNA c k testis adult male 8 week rRNA I ...
0
votes
1answer
569 views

MySQL Select with function IN () with bash array

How can I do this with an array? (without storing the array as regular variable) VAR='String1 String2 String3' SELECT * FROM db.table WHERE xy IN ( ${VAR// /, } ); not working example: SELECT * ...
1
vote
2answers
322 views

Using awk to process multiple files need to count occurance of variable after pattern. How can I stop array resetting after each file?

I want to count all occurances of any variable in parenthesis after patterns, for files in a directory that have a specific extension. Each file may contain the pattern on multiple records/line. First ...
0
votes
1answer
585 views

mdadm mounting array no such file or directory error

I used NAS system(Readynas) with Raid5 , but somehow trouble happened. So I connected every four disks to my Ubuntu machine and trying to rescue data. $cat /etc/mdstat/mdstat.conf ARRAY /dev/md/1 ...
0
votes
0answers
346 views

passing filenames as arguments to a SLURM job

I would like to pass input and output filenames for an R script using arguments in the SLURM file. These file names are in the variables InpfileList and OutFileList. I have tried the below code ...
0
votes
1answer
167 views

Counting and summing (and averaging) in an awk array

I have a data file that contains dates and corresponding measurements of some quantity. The full file stretches over 13 years and has ~4500 data points-- I have given an example of the format below. ...
1
vote
2answers
240 views

Function to iterate over array

I am using the following script, so as to call a function that is supposed to iterate over an array. #!/bin/bash function iterarr { for item in "$1" do echo "$item" done } myarr=...
0
votes
1answer
115 views

When to use arrays to define commands?

I read somewhere that arrays are best for making commands. sent='A long sentence with lots of words in it.' long=('-e' '/ long/d') lots=('-e' '/ lots of/d') init=('-e' '/ in it/d') echo sent | sed -r ...