A array is the simplest data-structure for storing items in continuously memory
0
votes
0answers
16 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
18 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
16 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
20 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
51 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
43 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
31 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
163 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
39 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
46 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
551 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
45 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
28 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
42 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
36 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
38 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
215 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
44 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
233 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
35 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
49 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
43 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
167 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
45 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
186 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
89 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
32 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
85 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
29 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
48 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
120 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
140 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
28 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
33 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
74 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
54 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
149 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
74 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
153 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
149 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 ...
1
vote
2answers
97 views
Store array to file and load array from file in BASH [closed]
I want to be able to store multiple integer arrays into a txt file when I'm done updating them, and then be able to load these arrays from the txt file into the script that I am using.
The arrays ...
5
votes
1answer
337 views
Single parenthesis in bash variable assignment
So this might seem like a stupid question, but I kinda have OCD about this kind of stuff and I was wondering about single parentheses in bash. I know that they are used for executing commands in ...
3
votes
1answer
264 views
Loop through a multidimensional array in bash 4
I want to define a hash list in bash (version 4.3.30):
4 gateways
each gateway has
an IP
an IP6
a name
...
and I want to walk through this list in a loop and do stuff to each gateway.
I ...
1
vote
1answer
40 views
Handle wildcards matching no file in bash
I am trying to read files from a directory into an array but even when the file doesn't exist, it is saved into the array. I want to exclude the file name if it doesn't exist.
a=(/tmp/nofileexists) ...
3
votes
4answers
337 views
Processing array values in bash
I am trying to create an array based upon filenames, and get in trouble with whitespaces. This seems common. But - as far as I can see - the quotes are set correctly, I guess it must be the way the ...
4
votes
3answers
126 views
Go from a String to an Array of “words” in Bash
I need to go from a string to an array where each entry is each word on that string. For example, starting with:
VotePedro="Vote for Pedro"
I need the array:
Vote
For
Pedro
Which I should then ...
1
vote
3answers
270 views
Put big data of heterogenous byte offset into arrays by AWK
Assume the data consists of byte offset which is not fixed i.e. the distance of two subsequent file headers varies.
The point of this thread is to go through each size of events separately in arrays. ...