A array is the simplest data-structure for storing items in continuously memory
1
vote
2answers
45 views
sh + how to use array in sh script in order to print all values in array
I want to use arrays in my sh script.
My target is to create an array with the values a b c and print all values in the array.
I succeeded to print each array, but I failed to print all values in ...
2
votes
4answers
100 views
How to initialize an array using awk and bash?
I am trying to store values of the first line of a text file into an array. Here is what I have so far:
arr_values=()
awk '
NR==1 {
for (i=0; i<=NF; i++)
...
6
votes
2answers
330 views
Array in unix Bourne Shell
I am trying to implement array in Bourne shell (/bin/sh). I found that way to initialize array elements is:
arr=(1 2 3)
But it is encountering an error:
syntax error at line 8: `arr=' unexpected
...
0
votes
1answer
152 views
How to do array addition multiplication program in a shell script? [closed]
Write a program arr_calc to perform array addition and array multiplication,
The program takes 4 arguments:
Argument 1: The file name of the input array A
Argument 2: The file name of the input array ...
1
vote
1answer
20 views
Bash array declared in a function is not available outside the function
on bash (v4.3.11) terminal type this:
function FUNCtst() { declare -A astr; astr=([a]="1k" [b]="2k" ); declare -p astr; };FUNCtst;declare -p astr
(same thing below, just to be easier to read here)
...
2
votes
1answer
51 views
What is the difference between @ and * when referencing bash array values?
This Bash guide says:
If the index number is @ or *, all members of an array are referenced.
When I do this:
LIST=(1 2 3)
for i in "${LIST[@]}"; do
echo "example.$i "
done
Gives: example.1 ...
2
votes
4answers
1k 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
...
0
votes
2answers
79 views
Using regex in for loop
I'm looking to loop through a directory of files and match them with regex, then subsitute the appropriate character:
#somefile.txt
%somefile.txt
>somefile.txt
# arr=(\% \> \#) ;for f in *; do ...
1
vote
1answer
38 views
Error when constructing an associative array
I am trying to construct an associative array in bash
#!/bin/bash
declare -A hero_escore
hero_escore=( "Invoker_Anti-Mage : -44"
"Tinker_Nyx Assassin : 32"
"Troll Warlord_Drow Ranger : ...
2
votes
2answers
128 views
Creating an array with IFS set to a different value
I am trying to construct an array and write to a file in this format, i.e. the file's content should be something like this
hero_pairscore=( askdjfh sdf,sdlkfj lksf,dfgdf,dsflkgj,asdlkf ....)
Where ...
1
vote
2answers
78 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 ...
0
votes
2answers
44 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 ...
2
votes
3answers
150 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 ...
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
1answer
150 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
54 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
4answers
2k 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 ...
5
votes
2answers
329 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 ...
4
votes
2answers
1k 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
44 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
94 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
47 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
89 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
78 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
104 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
220 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
816 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
115 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
396 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 ...
5
votes
3answers
180 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
votes
1answer
56 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
37 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
532 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
474 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[@]}*********" #********************
...
3
votes
2answers
160 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 ...
6
votes
1answer
492 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
358 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
137 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};
4
votes
2answers
3k 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
2answers
3k 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 ...
1
vote
2answers
390 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
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 ...
0
votes
1answer
38 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
2
votes
3answers
1k 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 ...
1
vote
2answers
7k 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
127 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
4k 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
125 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
121 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 '"' ' ')
...
3
votes
1answer
242 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 ...