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

learn more… | top users | synonyms

0
votes
2answers
26 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 ...
3
votes
1answer
68 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 ...
1
vote
2answers
38 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
29 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 ...
1
vote
1answer
39 views

“bad array subscript” error when adding entry to an associative array

I thought I'd better start a new query for this, since my original query was fully answered (thank you!). I have two files which are both outputs of a postgresql command. The first ...
0
votes
1answer
19 views

display array in a function - not working

What am I missing here? I have created a simple array: declare -a appArray=( "item1 -a -b" "item2 -c -d" ) If I echo this I can see it all echo ${appArray[@]} > item1 -a -b item2 ...
1
vote
1answer
43 views

one-dimensional array in shell not working for indexes greater than 08

I have been wondering why the one-dimensional array in shell isn't working for the sub-script / index 08 and 09. How come this happen? $ cat check month_arr=(0 31 30 31 30 31 30 31 31 30 31 30 31) ...
0
votes
0answers
21 views

Array Creator Bash OS X not expected output

I am creating a script which will prompt a user to enter a location of a text file and based from that it will create an array. First Question The text file that I am using is sample.txt and it is ...
0
votes
1answer
19 views

creating an array, not working

I'm trying to create an array within a bash script. I am doing this: #!/bin/bash declare -a testArray1=('a/b/c.def -x -y -z','x/y/z.000 -a -b -c') echo "testArray1[0] = ${testArray1[0]}" echo ...
0
votes
1answer
20 views

How to set up a pre-defined variable with multiple directory paths to use with the find command

I am looking to have a script delete files that are older than 90 days in certain directories. As in, there are 8 directories that have different paths to them. I can run the find command on one ...
1
vote
1answer
21 views

How would I create this function to choose a random variable from an array?

Here is my attempt: rand_var() { printf "%s\n" "${${!1}[RANDOM % ${#${!1}[@]}]}" } And I run it like this array=("something" "somethingelse" "test") rand_var array However, it tells me bad ...
0
votes
3answers
63 views

`Syntax error: “(” unexpected` when creating an array

I have two (Debian) Linux servers. I am creating a shell script. On the first one I create an array thus: #!/bin/bash target_array=( "/home/user/direct/filename -p123 -r" ) That works fine. ...
1
vote
1answer
49 views

Can a bash array be used in place of eval set — “$params”?

I'm taking a look at the optparse library for bash option parsing, specifically this bit in the generated code: params="" while [ $# -ne 0 ]; do param="$1" shift case "$param" in ...
1
vote
1answer
32 views

Iterate through an array and add new items to the array

I have an array setup: target_array=( "item1 -a100 -b250 -caaa" "item2 -a110 -cbbb -d1sa" "item3 -d2sa -exxx -fyyy" ) I then iterate over the array and perform various actions: for ...
3
votes
7answers
165 views

How to insert a string in array with different manner

I have a string like below. str='014387650' Now i want to split this string like below and put the value in the array. A[0]=0 A[1]=01 A[2]=014 A[3]=0143 A[4]=01438 A[5]=014387 ...
1
vote
2answers
43 views

Bash - non-numeral arguments to [] operator(array)

I have a small script below; however, I don't quite understand the for loop and if statement. First, let's take a look at script: listFieldNumbers () { for ii in ${!field_number[@]}; do if [ $ii ...
2
votes
4answers
50 views

Does directory match array variable

I'd like to check to see if a directory contains an array of file extensions. I'm on Ubuntu using Bash. Something like : files=$(ls $1/*) extensions=$( txt pdf doc docx) if [[ -e $files[@] ...
11
votes
2answers
588 views

Is there a reason why the first element of a Zsh array is indexed by 1 instead of 0?

From my experience with modern programming and scripting languages, I believe most programmers are generally accustomed to referring to the first element of an array by 0 as index. Are there any ...
2
votes
2answers
75 views

Passing multiple of arguments with whitespaces through a script to ssh

I want to remove multiple files from remote server. I have all files under one array in a script and I call other script which will remove files. Let output of "${b[@]}" is: ...
0
votes
2answers
38 views

Using Timeout in a Script with One Command, but Multiple Hosts

I'm writing a script that SSH into a device, SCP a file over, names it according to the device name and then goes on to the next one. My problem is if a device is not reachable the script hangs ...
0
votes
2answers
48 views

Bash arrays - not working

Doing some formatting fun, playing with xargs and how it passes data to scripts etc, and I'm having a little trouble with creating an array. alias lstest='ls | xargs --delimiter="\n" ...
1
vote
3answers
38 views

How to check for the first element in an Array

I have the following Array Loop set up. It simply loops through a string and checks to see if each item matches a letter and sets some variables accordingly. #!/usr/bin/env bash IN="ItemName -a -b" ...
1
vote
1answer
41 views

rotate element of array in shell script

I wanted to rotate element of array in shell script Array=(11 22 33 44 55) i tried this Array[0]=${Array[$3]} but it is not working, i'm getting array as it was declared.Why? Finally i got it ...
0
votes
1answer
28 views

Take output of array, and put into new array (perl script)

I have an array set up to check different workstations to see if any logs are present, and if logs are present, to delete them. The problem is, every time I execute the script, it has to crawl through ...
0
votes
1answer
22 views

Assigning a command to an array in shell script isn't working?

When I run run the command in the terminal it assigns the array. $ FILES=($(ls ~/Desktop/TEST/)) $ echo ${FILES[@]} file1.txt file2.txt file3.txt But as soon as I close the terminal and run it in a ...
3
votes
2answers
257 views

Looping through a shell array given as parameter

I'm trying to write up a little helper script that will change permissions and ownership to some sites on a server. Right now, I can either pass in 1 site, or do all via simply skipping that ...
2
votes
1answer
58 views

How to store 10 random numbers in an array then echo that array?

Here is the part that generates the 10 random numbers. MAXCOUNT=10 count=1 while [ "$count" -le $MAXCOUNT ]; do number=$RANDOM let "count += 1" done Now how do I output this to an array and then ...
2
votes
3answers
124 views

Duplicate indexes value in array

Given the files below: file1: 7997,1 7997,2 7997,3 5114,1 5114,2 file2: 7997,52, 5114,12, 4221,52, How can I create an array from the 1st file that has the first column as indices and the ...
3
votes
3answers
268 views

Reading stdin into a bash array

I want to accomplish the equivalent of: list=() while read i; do list+=("$i") done <<<"$input" with IFS=$'\n' read -r -a list <<<"$input" What am I doing wrong? ...
1
vote
1answer
37 views

Match data from array to data from file?

I'm trying to match up the data from an array the lists the days of the week like this: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY The files data reads like this: Name1,Tuesday ...
1
vote
2answers
51 views

Bash compose array with the input of other arrays, respecting a specific loop order

I am trying to generate a new array, with the combination of other arrays, respecting a specific sequence. In pure Bash. Example: numbers=(0 1 2 3 4 5 6 7 8); colors=(red blue green); ...
1
vote
1answer
44 views

Shell script: Text files to array

I must use C-shell. I have a 40+ list of ip and hostname in text file. sat1 100.34.54.65 sat2 100.34.54.55 sat3 100.34.54.45 and so on.. i want to set the ip and hostname as list of array. The ...
6
votes
2answers
235 views

Count number of elements in bash array, where the name of the array is dynamic (i.e. stored in a variable)

Brief statement of the question: Is there built-in bash method to count number of elements in bash array, where the name of the array is dynamic (i.e. stored in a variable), without resorting to ...
2
votes
1answer
48 views

Passing a set of parameters to a program using a Bash variable

I'm trying to clean up my shell script by placing repeating parameters in a bash variable. The best approach I know is to put it in an array and pass it... but that doesn't seem to work. How can I ...
3
votes
3answers
190 views

How do I download all files listed in an array?

I want to download all files in this GitHub directory to /usr/share/enlightenment/data/config. I have this script: L=('e.cfg' 'e_randr.cfg' 'exehist.cfg' 'module.battery.cfg' 'module.clock.cfg' ...
3
votes
1answer
92 views

Controlling the order in which files are put into an array

I have a script that grabs a bunch of time-stamped files and puts them into an array to be processed by Mutt and emailed to me. My problem is that the files are just randomly placed into the array ...
3
votes
1answer
33 views

Storing result of cat as different index values

i have parsed a file using catand now I want to save the result of it in array. var1=$(cat abc.txt | grep .........) and the var1 looks like 33 23 51 11 16 43 5 50 4 2 12 29 32 28 47 41 7 20 38 ...
1
vote
2answers
93 views

Efficient way to search array in text file by AWK

I have one array SPLNO with approx 10k numbers.Now i want to search the subscriber number from MDN.TXT file (containing approx 1.5 lac record)from the array.if subscriber number found in array it will ...
1
vote
2answers
34 views

What does the `set — $args` line do here, and why does it behave differently between Zsh and Bash?

In the manpage for the version of getopt that comes with Mac OSX, an example is given that uses the construction args=$(getopt optstring $*); set -- $args. What does set -- $args do here? ...
0
votes
1answer
49 views

How to store file in an array

How can I store contents of a file in array, for example: my file has : a /n b /n c /n d /n I want to store each line in array, how can I? I'm using bash 3 on OSX.
3
votes
2answers
141 views

MDADM RAID 0 Failure

Last night I noticed my system clock on my Ubuntu Server was 5 minutes fast so I ran an 'ntpdate pool.ntp.org' command and went to bed. This morning I noticed that SAMBA shares were not working. ...
1
vote
1answer
155 views

Storing part of command line arguments into user array

I am able to do this, array=(2 46 7 4 2 1 1 1 23 4 5) store=(${array[*]:5:5}) echo ${store[@]} # print 1 1 1 23 4 5 Now instead of extracting the 5 elements from position 5 from a user array, I ...
1
vote
1answer
29 views

How do I access an item of an array in shell?

I am using terminal with MacOSX. I am new to shell and I need to do something with array. I read this entry introducing about unix array. I tried to access an array as its way, but failed: a=(1,2) ...
1
vote
1answer
36 views

Array from piped commands fails

First off, apologies if this has been asked before, I searched both here and StackOverflow, tried the man pages, and I'm still drawing a blank. I'm trying to write a script that will auto-mount our ...
0
votes
1answer
87 views

ARRAY- Accept user input and output the corresponding choice from array

I'm working on a script that has an array of around 10 elements. All I have to do is that the script will accept user input of minimum 0 elements and maximum 10 elements (of the array) and it will ...
0
votes
2answers
59 views

How to store whole a paragraph as one element of array in bash script

I am using the following code to read data from a file abcd.txt and try to store in an array parameters, I am able to store 619617 in parameter[0] and update in parameters[1] but in parameters[2] I am ...
3
votes
1answer
156 views

How do I append an item to an array in a pipeline? [duplicate]

This script should simply add a value to an array through a loop and then show all items of an array. #!/bin/bash data_file="$1" down=() counter=0 cat $data_file | while read line; do \ ...
2
votes
4answers
86 views

zsh: map command to array

suppose you have an array a=(foo 'bar baz') is there a more obvious way to apply a command/function to every array element and save the resulting strings into another array than this: b=() for e in ...
4
votes
1answer
156 views

Pass BASH array to diff like file contents

I have two bash arrays, say: arr1=( 1 2 3 ) arr2=( 1 2 A ) and I want to compare them using diff. How could I pass the arrays as if they were the contents of a file? I tried a few variations, but ...
-1
votes
2answers
194 views

Loop over columns and store values to associative arrays

I've got a text file containing two columns, like so: 26 0.000342231 27 0.000342231 28 0.000684463 29 0.00136893 30 0.00102669 31 0.00308008 32 0.00308008 33 0.00444901 34 0.00718686 35 0.00718686 36 ...