Questions tagged [bash-array]
The bash-array tag has no usage guidance.
2
votes
2answers
69 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 ...
1
vote
3answers
74 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
31 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
18 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
31 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
28 views
Counting and adding if some arrays has element at some index
Please advise if below can 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
60 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
80 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
38 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
1k 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 ...
10
votes
9answers
7k 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
339 views
Construct bash array with only string format
Simple question - asking for a friend - 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 ...