A array is the simplest data-structure for storing items in continuously memory
0
votes
1answer
53 views
Trying to access environment variable (array) declared in ~/.profile from shell script
I'm currently fighting with this problem.
I've already read some older posts here on SE, and got the conclusion that the only sane way to put a variable (even an array) is ~/.profile.
So OK, array ...
1
vote
4answers
113 views
How to split text file into records by first non-blank charecter using awk and print only records containing string
I have a text file with information separated in blocks. I would like to separate these blocks into records so that I can handle them separately.
This is my text file (file.txt):
Alarm Stats:
...
0
votes
1answer
35 views
Zsh script how to concatanate array elements with string
I have written a zsh script to automize an analysis in high energy physics and now I would like to use an element of the one of the defined array some string and another element of some another array ...
0
votes
1answer
21 views
Cannot access elements of an array in zsh
I am trying to write a script in zsh and I need to make use of arrays in order to achieve my aim in an elegant way. After reading some information over the Net, I tried to implement and access the ...
0
votes
3answers
151 views
Bad substitution error in zsh shell
Hi in my previous question I got clarity on how to use associative arrays in zsh shell.
But whenever I trigger the following command in my script
for KEY in ${!array[@]} to iterate amongst the keys ...
0
votes
1answer
50 views
typeset -A is giving error in script
I was using associative arrays in my script, hence I used to declare them by the
typeset -A <array_name> command, and it worked fine in the bash prompt
But when I use it in my script, I get ...
2
votes
2answers
59 views
Memory allocation for sparse array in awk
I have searched but didnt reach to any conclusion that when i defined an sparse array does it reserve all the contiguous memory upto maximum index or it allocate the memory at that particular index ...
1
vote
1answer
60 views
How do I add an associative array to a variable from an external ini file?
I'm modifying a simple script to add function and learn more about how to write bash scripts. Currently, the script creates associative arrays using a function:
declare -A site theme
add_site() {
...
0
votes
3answers
55 views
store the command output in an array and print one by one
This is my command:
cat httpd.conf | grep ^LogFormat | awk -F\" '{print $(NF)}'
Output of this:
commonsess
common
or can be any number of values, I need to store these values in an array and ...
0
votes
1answer
695 views
mdadm raid doesn't mount
I have a raid array defined in /etc/mdadm.conf like this:
ARRAY /dev/md0 devices=/dev/sdb6,/dev/sdc6
ARRAY /dev/md1 devices=/dev/sdb7,/dev/sdc7
but when I try to mount them, I get this:
# mount ...
5
votes
1answer
178 views
Delete an array in awk
In awk, I can clear an array with a loop, making it an empty array, which is equivalent to deleting it.
for (key in array) delete array[key];
Is there a simpler way? Can I completely delete an ...
1
vote
1answer
63 views
How can I use global variable arrays in aliases or functions
I am attempting to simplify repetitive work procedures. In doing so, I am trying to write a .bashrc script which will set global path variables that aliases can refer to. Disclaimer: I'm new to linux ...
4
votes
4answers
157 views
Indirection on a Bash array
Is there a possibility to write the following script without the loop?
IPv4_first=1.1.1.1
IPv4_second=2.2.2.2
IPv4_third=3.3.3.3
IPv4_all=()
for var in ${!IPv4_@}
do
IPv4_all+=(${!var})
done
...
1
vote
2answers
124 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
500 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
1k 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
...
1
vote
1answer
34 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)
...
4
votes
1answer
80 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 ...
0
votes
1answer
478 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 ...
0
votes
2answers
280 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
46 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
360 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 ...
0
votes
2answers
53 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
346 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
209 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
256 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
97 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 ...
5
votes
2answers
3k 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
52 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
110 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
88 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.
3
votes
2answers
142 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
103 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
151 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
350 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
1k 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
137 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
1k 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
78 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
40 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
...
2
votes
2answers
853 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
874 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
233 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
1k 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
491 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
259 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
4k 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
201 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
556 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 ...
4
votes
4answers
4k 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 ...