The array tag has no wiki summary.
5
votes
2answers
281 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 ...
5
votes
3answers
125 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[@]})?
5
votes
1answer
122 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 ...
5
votes
3answers
1k 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 ...
5
votes
1answer
104 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 '"' ' ')
...
5
votes
1answer
1k 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 ...
4
votes
1answer
42 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[@]}" ...
4
votes
2answers
145 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 ...
3
votes
2answers
685 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 ...
3
votes
2answers
793 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
...
3
votes
1answer
95 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 ...
3
votes
2answers
772 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
553 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 ...
3
votes
1answer
64 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
219 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, ...
2
votes
4answers
127 views
Bash - Multidimentional 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 ...
2
votes
3answers
2k views
Bash eval array variable name
Here is my bash case:
First case, this is what I want to do "aliasing" var with myvarA:
myvarA="variableA"
varname="A"
eval varAlias=\$"myvar"$varname
echo $varAlias
Second case for array variable ...
2
votes
4answers
439 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
...
2
votes
1answer
47 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};
2
votes
1answer
110 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?
2
votes
1answer
773 views
Array of string expanded to path?
Suppose I have the following initialization of bash array:
my_array=(
"/usr/bin"
"/usr/lib/*.so"
)
If I do iteration using:
for array_item in ${my_array[@]}
do
...
done
Then the ...
2
votes
2answers
51 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
3answers
258 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 ...
2
votes
2answers
39 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 ...
2
votes
1answer
180 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
2answers
243 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 ...
1
vote
2answers
101 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 ...
1
vote
2answers
2k 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 ...
1
vote
2answers
382 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 ...
1
vote
1answer
198 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: ...
1
vote
0answers
262 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() ...
0
votes
2answers
51 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[@]}*********" #********************
...
0
votes
1answer
31 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