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

learn more… | top users | synonyms

2
votes
1answer
45 views

Issue with array length in bash script

I am writing a script that stores some of the command line arguments as an array, and uses the array later, but I'm having an issues getting the correct length of the array in the script. In the ...
1
vote
0answers
13 views

removing spare from array using hpacucli

Whilst configuring a new array I have somehow accidentally added the same disk to 2 arrays: => ctrl all show config Smart Array P400 in Slot 1 (sn: PAFGK0P9SX036K) array A (SAS,...
-1
votes
1answer
36 views

Adding Function to array based on index [closed]

I am using Bash. I am trying to add a function to an array. I tried using: if [ $CONTROL -eq 0 ]; then array[$index]+=`function_name1` fi if [ $CONTROL -ge 1 ] && [ $CONTROL -le 20 ]; ...
0
votes
2answers
50 views

Define a set of values for a variable in shell script

I want to define a set of values for a single variable. E.g. singleDigit={0,1,2,3,4,5,6,7,8,9} If the user gives 10 it should exit with an error. How can this be achieved without using a loop, but ...
3
votes
1answer
40 views

Ordered by insertion Map in bash

Is there an ordered (by insertion) map in bash? I know there's an associative array data structure in bash, but when iterating over it, the order of the elements is not by insertion. Example below. ...
5
votes
2answers
138 views

Array Expansion

I am having unexpected results in expanding an array. I am hoping someone can shed some light on why I am seeing what I am seeing. I am trying to populate an array using fdisk -l to fill it and only ...
5
votes
4answers
91 views

Is there an easy way to do the equivalent of `sed …` replacing the same line with multiple values?

I have a file where I want to replace some variables with data from a shell script. -A INPUT -i lo -s @LOCAL_IP@ -j ACCEPT Here I want to replace @LOCAL_IP@ with an IP address, I use the following: ...
0
votes
1answer
26 views

parse snmpwalk output to array

Im trying to get network interfaces by doing a snmpwalk, but cannot separate in array, the variable stores all in element 0 ifname=(\$(snmpwalk -Oqv -v2c -c comm xxx.xxx.xxx.xxx 1.3.6.1.2.1.2.2.1.2))...
2
votes
2answers
98 views

Reading a file with IFS loop works only when no arrays are used

Using the following code, I'm trying to read a file into an array: GROUPS=() while IFS=: read -r g1 g2 g3 g4 do GROUPS+=("$g3") echo "$g3" done < /etc/group This doesn't work, it doesn'...
2
votes
1answer
51 views

GNU Parallel: How can I reference array elements?

This is my case scenario: luis@Balanceador:~$ echo ${array[@]} a b luis@Balanceador:~$ echo ${array[1]} a luis@Balanceador:~$ echo ${array[2]} b luis@Balanceador:~$ parallel echo ${array[]} ::: 1 2 -...
1
vote
0answers
17 views

Returning an array from a ksh93 function

I would like to return an array from a function in ksh93. At this point, using the following method, the contents of the array are passed as a single element even though, in the fonction, the array ...
0
votes
0answers
40 views

How to resolve a variable's value inside an array in bash

I have a few files whose names I have assigned into variables. (There may be n number of files). Those variables are passed into an array. Now I want to print the names of the original files using ...
0
votes
3answers
88 views

Bash array values like variables inside loop

Im trying double loop using array values like array names for loop array1="name1 name2" name1="one two" name2="red blue" for name in $array1 do for value in $name do echo "$name - $value" ...
2
votes
2answers
36 views

Build a process substitution list for tee

I have a script like this which I use to broadcast commands to several instaces of the postgresql database CLI connected to several different servers. I'm using a hard-coded set of process-...
0
votes
2answers
33 views

How can I assign the last element of an array to a another array?

Let say I have an array $Info whose elements are ; ! total energy = -1090.13343774 Ry ! total energy = -1090.20757070 Ry ! total energy = -1090....
-4
votes
2answers
101 views

What is wrong in this code? [closed]

The code is ; cm=$1 nm=$2 case $cm in "out") declare -a en declare -a inf ec=$(grep -n "! " hw1_out_si_wire.txt) IFS=$'\n' en=($ec) lst=$((${#en[@]} -1)) IFS=' ' inf=($en[$lst]) echo " Energy: ${inf[...
0
votes
1answer
28 views

Why does the output look like different?

After executing this script, sometimes the output looks like this: 1912:! total e ergy = -1090.13343774 Ry 2310:! total e ergy = -1090.20757070 Ry 2709:! total e ...
1
vote
1answer
43 views

Array indexing using “sed”

I am working on one shell script which search directory modified in last 24 hours and then compress them using tar. Folder Search : find /path to log directory/ -maxdepth 1 -type d -mtime +0 ...
-1
votes
2answers
165 views

How to sum many numbers inside 2D array using awk

I need to sum numbers located in a file like this one: column1 column2 column3 row1 a(1,1) a(1,2) a(1,3) row2 a(2,1) a(2,2) a(2,3) row3 a(3,1) a(3,2) a(3,3) row4 a(4,1) ...
2
votes
1answer
74 views

How to sum numbers inside many 2D arrays

I need to sum numbers located in a file like this one: column1 column2 column3 row1 a(1,1) a(1,2) a(1,3) row2 a(2,1) a(2,2) a(2,3) row3 a(3,1) a(3,2) a(3,3) row4 a(4,1) a(4,2) ...
3
votes
2answers
42 views

Looking for a convenient way to create a “constant array”

In zsh, is there a convenient way to create an array of n copies of the same integer? Context: In a shell script, I need to create an array, tile_lengths, consisting of two longish constant blocks. ...
0
votes
0answers
38 views

Array value won't get printed out

I have the below bash script, but rather that I get the command executed stored in cmd[1] it prints [1]: command not found. I executed the command with the following way: $ qsub -J 1-33 sicer-pbs.sh ...
2
votes
4answers
80 views

String Manipulation Shell Script [duplicate]

I am working on a UPS Monitoring project using NUT Server. My objective is to make a shell script which sends one command and in response receives the status and other parameters from the UPS. for ...
1
vote
1answer
54 views

bash script read array outside loop

This is my first attempt to write a bash script and I'm unable to read array outside for loop. What I'm trying to do is.. store name of all files from /MyDir in an array. Check if there is process ...
2
votes
2answers
64 views

Split words from `read` and store to array?

How can I take input from read, split the words up by spaces, and then put those words into an array? What I want is: $ read sentence this is a sentence $ echo $sentence[1] this $ echo $sentence[2] ...
2
votes
2answers
63 views

find duplicate in first, second columns and sum numbers in third column

I have this input: Fred apples 20 Susy oranges 5 Mark watermellons 12 Robert pears 4 Robert oranges 17 Terry oranges 9 Lisa peaches 7 Susy oranges 12 Mark grapes 39 Anne mangoes 7 Greg pineapples 3 ...
0
votes
1answer
61 views

Match words in array line by line to a seperate file

Here is a sample of my input data: $9.99,Titan the Power,www.example.com,565654 $15.99,Conan The Barbarian,www.sure.com,565438 $1.99,Julia Cesar,www.bfg.com,69722 I have written this code so ...
3
votes
1answer
34 views

why this pattern replacement is not working on array?

for a variable z z="I II III IV V VI VII VIII IX X XI XII XIII XIV XV XVI XVII XVIII XIX XX" echo "${z// /,}" output is I,II,III,IV,V,VI,VII,VIII,IX,X,XI,XII,XIII,XIV,XV,XVI,XVII,XVIII,XIX,XXOK. ...
0
votes
1answer
29 views

how to get the size of an indirect array in bash?

Without using eval. this wont work: astr=(a b c) str="#astr[@]" echo "${!str}"
0
votes
2answers
40 views

When does an array variable represent the entire array, and when the element indexed by `0`?

Bash manual says: Referencing an array variable without a subscript is equivalent to referencing with a subscript of 0. In Bash or POSIX shell, is there some general rule for when an array ...
1
vote
0answers
25 views

Access to bash arrays in gnuplot [duplicate]

In a bash script I have several arrays which I need to access in a gnuplot code: #!/bin/bash ... # fill array a[*] and b[*] ... gnuplot <<EOF do for [j=0:10] { # access ${a[j]} and ${b[j]} } ...
1
vote
3answers
36 views

Find position of matched word in a line in bash (not position of character)

I have a line like Xckt VDD VSS sig[1] sig[2] sig[3] sig[4] sig[5] sig[6] CKT and I want to find the position of sig[4] in the line. In this case it should return 7. I wanted some simple script/ ...
3
votes
3answers
68 views

How to pass array to bash shell script?

How do I pass an array as a variable from a first bash shell script to a second script. first.sh #!/bin/bash AR=('foo' 'bar' 'baz' 'bat') sh second.sh "$AR" # foo sh second.sh "${AR[@]}" # foo ...
3
votes
2answers
66 views

use printf to format output of an array

I have an array that contains details about each NIC. Each array index consists of three space-separated values. I would like to have a nice table as output. Is there a way to assign formatting ...
0
votes
0answers
13 views

Declaring multiple arrays from contents in multiple directories, then iterating over those arrays and their contents [duplicate]

In a bash script, I'm trying to declare 15 separate arrays with the contents of a folder for 15 separate folders (IE; array1 = contents of folder1, array2 = contents of folder 2, ..., array15 = ...
2
votes
1answer
69 views

POSIX Awk sort array

I would like to sort an array with POSIX Awk, however I have discovered that: asort() asorti() PROCINFO["sorted_in"] are all GNU extensions. Can this be done with without GNU Awk?
2
votes
1answer
42 views

Bash script to summarize “ last -a ”

I would like to make a shell script such that it runs the " last -a " command and summarizes it's data in the form : userID : number of login sessions Host or ip1 - number of ...
0
votes
3answers
34 views

How can I designate range within an column when making an awk file?

I have this data file which I want to sort out by making an awk file. I want to find the average rate within certain range of ages. For example, from 10<age<20and 20<age<30. And I can't ...
0
votes
2answers
57 views

Range arrays with custom step-size in Bourne shells

Say I want to create a range array of integers in a shell such that the array: Starts in $START_VALUE Ends in $END_VALUE Elements are $STEP_SIZE apart, starting from $START_VALUE. For anyone ...
1
vote
0answers
45 views

Assigning floating point value to array in Unix shell script loop [closed]

I am using ksh scripting on Ubuntu 14.04 and am trying to write a script to add floating point numbers to an array. My code is as follows numLoops=6 numerator=14996288 denominator=1000000000 for i ...
1
vote
2answers
68 views

Bash substring expansion of array with some unset element

I just discovered a weird behavior when indexing into bash arrays with unset elements. I make an array with these three elements: $ arry[3]=a $ arry[4]=b $ arry[5]=c The array appears to be right: ...
3
votes
1answer
59 views

On passing arguments to programs through array variables

NB: though the question below features rsync, it is not a question about rsync; it is a question about zsh arrays. If I initialize the variable EXCLUDES like this EXCLUDES=( --exclude=/foo --...
0
votes
2answers
109 views

create an array of dates in bash scripts and match it current date

Actually I want to create an array of dates and compare it to the today date if it match with the current date then execute test.sh file otherwise exit the loop in bash script. I did like this... #!/...
0
votes
2answers
93 views

How to get the psql table records in shell script?

I have postgresql, in that I have a table with 10 records, I want the 10 records in 10 local variables shell script. I tried with following way, but it will store all the records in list123[0] ...
2
votes
1answer
70 views

read 'df -h' output as array of lines in bash

I am trying to read "df -h" output as array of lines in shell script. I tried the below code, but its var[0] is giving me the first column of first line instead of the whole first line. My ...
3
votes
3answers
83 views

bash array with variable in the name

I'll appreciate your help with the following issue: I'm trying to set an array which contains a variable as part of the array name, example: Arr_$COUNTER (where $COUNTER is changed based on a loop ...
0
votes
2answers
102 views

Bash: split multi line input into array

I've got a file with strings and base64 encoded data over multiple lines, that are sepearated by a comma. Example: 1,meV9ivU4PqEKNpo5Q2u2U0h9owUn4Y8CF83TTjUNWTRQs7dEgVxnsMgf4lvg9kvxcIaM3yB4Ssim ...
4
votes
1answer
149 views

Append (alter) each array element via parameter expansion (i.e. without printf)?

Let the script below exemplify my quandary.. #!/bin/zsh STUFF=( moose-hoof ovary clydsedale ) echo ${MINE=$(printf "MY-%s " $STUFF)} echo ${MINE_EXP=${STUFF/^/MY-}} MY-moose-hoof MY-ovary MY-...
1
vote
2answers
70 views

Pass Array to SSH Connection

How can I pass an array across the ssh connection. I have an array formed thus: declare -a target_array=( "item1 -p12345 -r" "item2 -p65677 -e" ) I then need to pass it to an ssh ...
1
vote
2answers
81 views

Generate unique numbers RANDOM function OS X

I am creating a random generator based from a number array list. However, I would like it to be unique so that random number cannot be used again. I have tried creating a 'USED' array list which will ...