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

learn more… | top users | synonyms

0
votes
2answers
41 views

Reversing 1-D array

I'm trying to reverse a 1-D array which contains a list of files. I followed http://stackoverflow.com/questions/13360091/how-to-reverse-array-in-bash-onliner-for-loop to be the code. But this is not ...
1
vote
2answers
34 views

get arguments passed and put it in an array

is there a way to make the arguments passed become the element of an array? I want to access those arguments individually through array. like this: ./script.ksh arg1 arg2 arg3 then it will become ...
2
votes
3answers
110 views

List all word pairs that appear in a line together in a file

How can I use awk to run through a file written as: Sarah,Masha,Helen Connor,Jessica,Jennifer,Candace Betty,Sarah,Helen John,Harold,Frank,Daisy and put out all pairs that appeared in a line ...
2
votes
1answer
113 views

What is an array in awk?

I'm very new to unix and am trying to learn basic awk scripting - during which I keep coming across the word "array", and "associative arrays". I have no clue as to what this is, and don't tell me to ...
1
vote
1answer
34 views

Is there a simple way to get array of all arguments that do not begin with a hyphen?

zsh includes a powerful utility for parsing command-line options zparseopts, but for quick shell scripts and shell functions, but is there an easy way to extract the array of all command-line ...
3
votes
2answers
227 views

Associative Arrays in Shell Scripts

I saw a trick for implementing associative arrays in a shell script. For example print array["apples"] could be scripted as echo \$array$key where key=apples. However, there was no mention of how to ...
1
vote
2answers
43 views

Is this an array?

I'm currently looking at an existing script that have something like this: variable='val1 val2 val3 val4' Is this another way of setting an array? If not, what is it?
1
vote
1answer
78 views

Bash Array does not store correctly

I have a problem with my bash array I was trying to read the file however the array allocated it in array[1] instead of 0 #!/bin/bash index=0 INPUT=BookDB.txt OLDIFS=$IFS IFS=: i=0 book=() [ ! -f ...
1
vote
1answer
22 views

Count number of elements in an array

I would like to ask if there is a way to count the number of elements in an array in ksh.
1
vote
2answers
64 views

How do you preserve a space in a literal expression in an array value in bash and how to trim down results from a sort command?

In the context of searching for expressions in log files, I was wondering generally if there was a way to quantify and qualify the contents of the logs in /var/log in some way. In particular, does the ...
1
vote
1answer
49 views

Attempt to fake a 2D multidimentional array in bash by storing the array name as string

I am trying to create a structure that will allow me to group data inside an array in bash Here is what I have tried: indexes=() first_element=( value='one' another_value='two' ...
2
votes
2answers
66 views

Bash scripting: Associated Array only recognizes one value when several given

This lines of a Bash (4.0) Script klang=([string_0]='element_0' [string_1]='element_1' [string_2]='element_2') echo "${klang[*]}" # should output the all set elements echo "${#klang[*]}" # ...
1
vote
1answer
128 views

Problem with sed on a array containing strings containing spaces

I have a array looking like this: array=("(1 2 3) (123)" "2 31 (231)" "4 5 1 (451)" "(te)xt (1234)") This array is a example. It does not look like this but its structure is the same (the strings ...
6
votes
4answers
350 views

Intersection of two arrays in BASH

I have two arrays like this: A=(vol-175a3b54 vol-382c477b vol-8c027acf vol-93d6fed0 vol-71600106 vol-79f7970e vol-e3d6a894 vol-d9d6a8ae vol-8dbbc2fa vol-98c2bbef vol-ae7ed9e3 vol-5540e618 ...
0
votes
1answer
103 views

I need Control loop help -

this is what I figured out in sorting an Array that has already had it's elements declared type int #!/bin/bash #sorting an "predeclared" array typeset -i store BigNum let store=0 BigNum=0 ...
0
votes
1answer
136 views

How to reassign new values to array elements?

I'm trying to figure out like three or four things at the same time.The most I need help with is how to get the greatest number in all the files that I have created on a prior run of my script, that ...
-1
votes
1answer
51 views

find not working right

if [[ "$1" == "" ]] then leftarray=($(find . -type l -printf "%p\n" 2>/dev/null)) rightarray=($(find . -type l -printf "%l\n" 2>/dev/null)) for var in "${rightarray[@]}" do ...
1
vote
1answer
32 views

for + array + args

I have problem with my for cycle, this is what I want: for arg do array[index]=(`find $arg -type l -maxdepth $depth -printf "%p\n" `) index++ done fi arg - positional arguments, only directories ...
1
vote
2answers
322 views

How to sum a bash array of numbers (some in scientific notation)?

Since the following command using bc does not work for numbers in scientific notation, I was wondering about an alternative, e.g. using awk? sum=$( IFS="+"; bc <<< "${arrValues[*]}" )
0
votes
2answers
240 views

Why does this syntax not work for declaring associative array [closed]

Can i use a variable inside () syntax #!/bin/bash declare -A c declare -A b a="[a]=0 [b]=1 [c]=2 [d]=3" b=($a) # or b=($(echo "$a")) echo "***********${b[@]}*********" #******************** ...
2
votes
2answers
125 views

bash: get array name from parameter to function with saving indexes

I have a function to show index of chosen element. I'm trying to pass a parameter to function to use it as an array name. This works: getIndex() { arrname=$1[@] b=("${!arrname}") index=1; while ...
5
votes
1answer
196 views

is there a way to list all 'indexes IDs' (keys) on a bash associative array variable?

I have this array: declare -A astr I add elements to it: astr[elemA]=123 astr[elemB]=199 But later on I need to know what are the indexes IDs (elemA and elemB) and list them. echo "${astr[@]}" ...
2
votes
2answers
267 views

How can I add an array of arguments inside my rsync call inside a script?

I want to copy a folder to another location, while excluding some specific files Here is my current script: #!/bin/bash if [ -n "$2" ] then source=$(readlink -f $1) destination=$(readlink ...
2
votes
1answer
99 views

Bash: slice of positional parameters

How can I get a slice of $@ in Bash without first having to copy all positional parameters to another array like this? argv=($@) echo ${argv[@]:2};
3
votes
2answers
2k views

C Shell Array Declaration Syntax, () vs {}

In my C-shell script (tcsh specifically) I have been trying to declare an array with six values: x, nx, y, ny, z, nz. After some trial and error I discovered three different ways of declaring the ...
5
votes
3answers
172 views

${#array} vs ${#array[@]}

As far as I can tell both ${#array[@]} and ${#array} evaluate to the number of elements in $array. Is there any reason for preferring the longer form (${#array[@]})?
1
vote
2answers
298 views

for loop to get more than one arguments

In python and some other programming languages, it is easy to get a vector instead of one variable everywhere and in loops. like python: for variable in ...
3
votes
4answers
1k views

Bash - Multidimensional Arrays and Extracting variables from output

I am trying to do something simple however I'm not sure how to achieve my goal here. I am trying to extract the: USER, TTY and FROM values that are given by the w command on the console. In bash I am ...
0
votes
1answer
37 views

Disconnect a disk correctly

I wanna disconnect a disk from an array which is the correct process umount file system remove from volume group i dont know this step disconnect physically the disk
1
vote
2answers
980 views

How to unset range of array in Bash

I'm trying to delete range of array element but it's fail.. My array root@ubuntu:~/work# echo ${a[@]} cocacola.com airtel.com pepsi.com Print 0-1 array looks ok root@ubuntu:~/work# echo ...
2
votes
3answers
771 views

Passing multiple directories to the -prune option in find

I am using find to locate and delete backup files but wish to exclude certain directories from the search. The backup filenames could terminate in .bck, bak, ~, or backup. The Minimal Working Example ...
5
votes
2answers
238 views

Gawk: Passing arrays to functions

Stuck with GNU awk 3.1.6 and think I've worked around its array bugs but still have what looks like a scope problem in a 600-line awk program. Need to verify understanding of array scope in awk to ...
1
vote
2answers
5k views

BASH reading txt file and storing in array

I'm writing my first BASH script , I have some experience with c and c# so I think the logic of the program is correct..it's just the syntax is so complicated because apparently there are billions of ...
5
votes
1answer
126 views

Find all users who have more than N processes and echo them in shell

I'm writing a script in ksh. I need to find all users who have more than N processes and echo them in the shell. N is read from ksh. I know that I should use ps -elf, but how do I parse it, find ...
4
votes
2answers
2k views

How can I remove an element from an array completely?

unset array[0] removes the element but still if I do echo ${array[0]} I get a null value moreover there are other ways of doing this but if an element of an array contains spaces like below ...
4
votes
2answers
3k views

Bash script wait for processes and get return code

I am trying to create a script which will start many background command. For each background command I need to get the return code. I have been trying the following script : #!/bin/bash set -x ...
3
votes
1answer
118 views

Bash 3.0 not supporting lists?

I have written a small script that add particular IP addresses taken from a config file and then puts it in a list : WAS_IP=$(grep "<was_ip>" $CONFIG| cut -d '>' -f 2 | cut -d '<' -f 1 ...
5
votes
1answer
117 views

array[@] output all messed up?

I've got this code: Unix+=("Stock List") while read line; do result=$(wget -O - -o /dev/null "http://finance.yahoo.com/d/quotes.csv?s=$line&f=sl1&e=.csv" | tr ',' ' ' | tr '"' ' ') ...
2
votes
1answer
130 views

Zsh style arrays with Bash

Does Bash have a way to access arrays similar to Zsh, something like $ foo=(dog cat mouse) $ echo $foo[1] cat instead of $ echo ${foo[1]} perhaps using some shopt setting?
3
votes
1answer
217 views

Why is not every variable initialized to its data type specific default value upon declaration in Bash?

When I execute the following code in Bash version "GNU bash, version 4.1.10(4)-release (i686-pc-cygwin)", I get: declare a declare -p a # Output: -bash: declare: a: not found declare -i b declare -p ...
1
vote
1answer
293 views

How to initialize a read-only, global, associative array in Bash?

I execute the following code in Bash version "GNU bash, Version 4.2.39(1)-release (x86_64-redhat-linux-gnu)": function foobar { declare -rgA FOOBAR=([foo]=bar) } foobar declare -p FOOBAR # Output: ...
5
votes
2answers
376 views

Strange behaviour of uninitialized arrays and unset arrays

I'm writing a script and I discovered some unexpected behaviour of uninitialized and unset array variables that I do not understand. First of all, the length: giacomo@jack-laptop:~$ echo ...
3
votes
1answer
794 views

gnuplot shell variable substitution and arrays

I need to use shell variables in my gnuplot commands, for which I'm using the here document style. I also need to use loops inside the gnuplot code. Both these things are working. Now -- I want to ...
1
vote
2answers
489 views

Auto-expansion problem with array elements containing an '*' (asterisk)

I'm trying to write me a find script that should later be able to read a list of directories to be excluded from an external file. Whilst I can accomplish that part myself, it's the annoying array ...
5
votes
2answers
2k views

What is the most correct way to pass an array to a function?

Consider I have a very large array $large_list, is there a way to write a function that will take the array as an argument? For example: echo_idx_array () { arr="$1" idx="$2" echo ...
2
votes
4answers
956 views

Loop with 2 variables in a bash script

I am trying to utilize an API. I need to either do some type of "for loop" that replaces or utilizes 2 variables... In pseudo.. # Declare New Servers newserver=( box001 box002 box003 box004 box005 ...
5
votes
3answers
2k views

Transform an array into arguments of a command?

I have an array of "options" of a command. my_array=(option1 option2 option3) I want to call this command in a bash script, using the values from array as options. So, command $(some magic here ...
3
votes
1answer
67 views

Calling a script on each line of a file

I have a bash script that takes 3 arguments: $ do_something foo bar baz and a file with several lines, each with different 3 args. I want to execute the script as a cronjob, and each time it's ...
3
votes
1answer
258 views

Write default array to variable in Bash

I was expecting excludes="${excludes:-( ${default_excludes[@]} )}" to be an array if $excludes is empty. Unfortunately the stuff after :- is taken to be a string. Did I miss some syntax contortion, ...
1
vote
0answers
281 views

Array Performance very similar to LinkedList - What gives? [closed]

So the title is somewhat misleading... I'll keep this simple: I'm comparing these two data structures: An array, whereby it starts at size 1, and for each subsequent addition, there is a realloc() ...