Questions tagged [bash-array]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
2answers
21 views

checking if a rpm package exists in an array

I am trying to find out if certain RPM packages exists in an array. if any of the package is missing it will exit with the message "Package doesnt exist" .Here is my code #!/bin/bash echo "Checking ...
2
votes
1answer
64 views

Bash array only executes first index

I am working with a server running Ubuntu 18.01 LTS and I'm trying to automate the backup of multiple virtual machines. I have the VM names in an array and then a for loop to shut down, backup and ...
0
votes
1answer
26 views

Reference items in bash for loop from find command

Suppose I have this code: for i in $(find * -type f -name "*.txt"); do # echo [element by it's index] done How do I access, if possible, an element by it's index?
0
votes
1answer
36 views

issue with bash arrays containing both commands and paths

I'm trying to write a script to tailor my dotfile setup to each machine using git update-index -skip-worktree but it keeps chopping up the paths. I'm passing arrays starting with commands where every ...
1
vote
1answer
98 views

Why is “${ARRAY[@]}” expanded into multiple words, when it's quoted?

I don't understand why "${ARRAY[@]}" gets expanded to multiple words, when it's quoted ("...")? Take this example: IFS=":" read -ra ARRAY <<< "foo:bar:baz" for e in "${ARRAY[@]}"; do echo $...
1
vote
0answers
40 views

Assigning array values while running a loop to a variable dynamically

The values present in indexArray are: 1 4 3 2 Below is the code snippet(This is not the complete code): while read -r line;do position=${indexArray[$counter]} # No value is assigned to "position" ...
1
vote
1answer
30 views

Array only returns one element

i'm trying to generate a script that ftp some files to a server using lftp. when i run these commands in shell: DBNAME=TESTDB ls -t /data*/${DBNAME,,}Backup/$DBNAME.0.db21.DBPART000.`date +%Y%m%d`...
1
vote
2answers
258 views

Merge duplicate keys in associative array BASH

I've got an array that contains duplicate items, e.g. THE_LIST=( "'item1' 'data1 data2'" "'item1' 'data2 data3'" "'item2' 'data4'" ) Based on the above, I want to create an associative array that ...
9
votes
3answers
971 views

How to remove new line added by readarray when using a delimiter?

VAR=a,b,c,d # VAR=$(echo $VAR|tr -d '\n') echo "[$VAR]" readarray -td, ARR<<< "$VAR" declare -p ARR Result: [a,b,c,d] declare -a ARR=([0]="a" [1]="b" [2]="c" [3]=$'d\n') How can I tell ...
5
votes
1answer
152 views

How to pipe multiple results into a command?

I have a piece of code which works, something like this (note this is inside CloudFormation Template for AWS auto deployment): EFS_SERVER_IPS_ARRAY=( $(aws efs describe-mount-targets --file-system-id ...
1
vote
2answers
59 views

Pass the name of an array in command line to reference the array in a function

I'm trying to learn more bash by updating my bash_profile so that I can quickly do some adb commands that I usually have to copy-paste. I found I was creating many similar functions that all looked ...
1
vote
2answers
419 views

gnu parallel with bash array

I trying to run command recon-all with GNU parallel freesurfer preproc i have a bash array of list of patients to run 8 patents simultaneously: root@4d8896dfec6c:/tmp# echo ${ids[@]} G001 G002 G003 ...
2
votes
2answers
1k views

Find array length in zsh script

Is there a way to find the length of the array *(files names) in zsh without using a for loop to increment some variable? I naively tried echo ${#*[@]} but it didn't work. (bash syntax are welcome ...
2
votes
3answers
1k views

Find second largest value in array

I have an array like this: array=(1 2 7 6) and would like to search for the second largest value, with the output being secondGreatest=6 Is there any way to do this in bash?
1
vote
2answers
2k views

Shell script-How to return maximum value in array?

I have a array: ARRAY=(12.5 6.2) I wish to return the maximum value in ARRAY which Output is 12.5 Anyone can share me ideas? I have try this: max=0 for v in ${ARRAY[@]}; do if (( $v > $max ...
2
votes
1answer
30 views

How to reference an array with a function prior to bash4.3

If we: Define an array; and then.. Define a function; and want to.. Call that array from inside the function.. We can. Like so: Input: myArray=('1' '2' '3' '4' '5') myFunction () { local -n ...
0
votes
1answer
235 views

Array inside an Array: Different syntax for Array in bash

I found the following example from here. But I can't understand how is array arr is being defined. a='domain.de;de;https' $ arr=(${a//;/ }) What is the advantage of defining it like this? Actually, ...
1
vote
2answers
51 views

Counting and adding if some arrays has element at some index

Can the below code be easily achieved with minimum coding. $ cluster1=(x y) $ cluster2=(a b) $ cluster3=(m) $ my=$((${cluster1[0]+1}+${cluster2[0]+1}+${cluster2[0]+1})) $ echo $my 3 $ my=$((${...
2
votes
2answers
71 views

How to access further members of an array when using bash variable indirection?

Consider the following example, it seems it's working fine with the index 0: $ a1=(1 2 3) $ a2=(a b c) $ for x in a1 a2; do echo "${!x}"; done 1 a $ for x in a1 a2; do echo "${!x[0]}"; done 1 a ...
0
votes
2answers
358 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
96 views

Greping Load Averages

I'm trying to grep all load averages & put them in an array. What should be the exact way, keeping in mind the followings requirements. Defining Array: LA= ("one" "five" "fifteen") LA= (`(uptime ...
0
votes
1answer
3k 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 ...
17
votes
11answers
15k 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
0
votes
1answer
515 views

Construct bash array with only string format

Is there a way to pass a string value as env variable and have bash recognize it as an array? In other words, is there some special string format that tells bash that the string is an array? Or is ...