A array is the simplest data-structure for storing items in continuously memory
1
vote
1answer
31 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
58 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
49 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
60 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
33 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
25 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
39 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
33 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
36 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
61 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
57 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
39 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
33 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
45 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
42 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
62 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
56 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
71 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
53 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
47 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
62 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
60 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
111 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
62 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
59 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
201 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
26 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
55 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
37 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
49 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
24 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
175 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
64 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
179 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
50 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
58 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
700 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
241 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
169 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
73 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
54 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
67 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
25 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
463 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
117 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 ...