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

Questions tagged [array]

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

0
votes
1answer
28 views

bash share array in “for do () & wait” loop

I have an issue with the script below. It uses a few sql statements in a for loop to set variables and produces a log file. The current db produces +- 1200 lines of output. The original version is ...
3
votes
2answers
45 views

assign output of command to array

when i run this command: cat output | grep -i state | sort | uniq | awk '{print $ 3}') the output is: 00x1 00x5 0080 to aasign them to an array i did this: STATUS_ARRAY=($(cat output | grep -i state | ...
-2
votes
0answers
23 views

Changing an element in a char array [on hold]

Ok. Simple question. I'm working on a project, in Linux, for a program in C that requires arbitrary information in certain locations. Say I have an array: char *choices[] = { "...
18
votes
2answers
805 views

What type of operation is /#/- in “${my_array[@]/#/-}”?

The accepted answer to Transform an array into arguments of a command? uses the following Bash command: command "${my_array[@]/#/-}" "$1" I'm trying to figure out what the /#/- part does, exactly. ...
1
vote
2answers
72 views

tar “Cannot stat: No such file of directory” when passing array variables [closed]

I'm trying to write a bash script that will create an archive (tar) of files in a directory. The file extensions need to be passed in as arguments when calling the bash script (./backup.bash pdf txt ...
-1
votes
1answer
93 views

How to put whiptail parameters into an array / loop? [closed]

I have a small but embarrassing / annoying issue with coding whiptail command. I am passing "$x" parameters to function for whiptail to process. It works as expected , but looks ugly. I would like ...
4
votes
2answers
196 views

Bash scripting Array concepts

I have two separate lists. One contains 1000 hostnames and other contains 1000 associated IP's for that hosts. I need to generate config entries and push to my config file to add all hosts under our ...
0
votes
0answers
8 views

Python RegEx to Catch White Space Between Array Name and Size [migrated]

I am using a regular expression in a python script to search through a file to find variable declarations. This is what I have so far: ret1 = re.compile(r'^\s*(volatile|register|typedef)?\s?([\w<&...
0
votes
0answers
37 views

Why can't I mount array disks for dd?

When I have an USB disk recognized, I can do the 'dd' command and wipe data from the stick. I cannot do that with a SCSI disk from array such as NetApp. They don't seem to mount at all. The disk would ...
1
vote
2answers
56 views

to run a command for various variables in shell script

So, i'm storing the values of the first column returned from a sql query in a variable(var)..it's supposed to be an array type, because the sql query returns multiple rows, and later,these values ...
1
vote
1answer
33 views

How to delete existing elements from an array and keep appending new elements afterwards

Below is an example, where I am using two arrays which could be initiated with many elements, for now having "None" as the only element, is there any way to delete existing element/elements and then ...
1
vote
4answers
102 views

How to iterate through an array of numbers in shell-script?

I am trying to iterate through an array created after some command execution. The code used is: #!/bin/bash mailx -H|grep '^ [UN]'>ListOfMessages.txt msgNumbers=`cut -c 4-5 ListOfMessages.txt` ...
1
vote
1answer
50 views

Bash array using system positions limits? [closed]

Here is my array declaration arraySYSTEM=( "$@" ) It can have up to 10 "system position(?)" members. I'm using it to build text parameters passed to whiptail function. Then I select the parameter ...
0
votes
1answer
24 views

Find overlap/intersection of lists with bash

Say I have these two "lists": #!/usr/bin/env bash git fetch origin; first_list=( ); second_list=( ); git branch --merged "remotes/origin/dev" | tr -d ' *' | while read branch; do first_list+=(...
0
votes
1answer
27 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
570 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
128 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 ...
1
vote
7answers
104 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
80 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
95 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
43 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
54 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
61 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
2k 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
66 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
214 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
922 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
112 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
61 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
40 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
204 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
93 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
63 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
333 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
190 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
277 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
183 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
801 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
3k 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
262 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
113 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
26 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
839 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 ...
5
votes
4answers
2k 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
35 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
69 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-...