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

learn more… | top users | synonyms

1
vote
3answers
30 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
34 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
59 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
51 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?
1
vote
1answer
37 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
31 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 ...
-2
votes
0answers
6 views

remember checkbox value if its checked array name in a form php [migrated]

lets say we have this: echo '<form method="post"> <div class="form-group"> <table class="table table-bordered table-hover table-striped" style="width:auto"> ...
0
votes
0answers
6 views

Removing items in one array based on whats in another array - python [migrated]

I'm trying to make a function that removes items from all_cards based on whats in the iss_answer array. My iss_answer array will change often in my program so I can't just type ...
0
votes
2answers
43 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
39 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
58 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
54 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
60 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
40 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
42 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
54 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
50 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
102 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
58 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
55 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
153 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
24 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
52 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
36 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
21 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
43 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
144 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
59 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
34 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
174 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
49 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
54 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
682 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
198 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: ...
1
vote
2answers
110 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
68 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
51 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
63 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
32 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
24 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
391 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 ...
1
vote
1answer
93 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
132 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
475 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
40 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
56 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
46 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
794 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 ...