A array is the simplest data-structure for storing items in continuously memory
1
vote
1answer
45 views
create an array
I want to learn about arrays and how to assign values to them, so I found this tutorial
While running the following script:
#!/bin/bash
$names=([0]="Bob" [1]="Peter" [20]="$USER" [21]="Big Bad ...
-1
votes
1answer
49 views
Accessing array elements within process substitution bash
I've got a text file which looks like this:
b4238ca2-cb8d-11e4-8731-1681e6b88ec1,https,username,password,ipaddress
b4238f0e-cb8d-11e4-8731-1681e6b88ec1,https,username,password,ipaddress
...
1
vote
1answer
27 views
Setting Qualifiers for Bash Array
I have a bash script that is running an SNMPGET of two values. I want to take the results and put them in an array.
Here is the code:
OUTPUT=`snmpget -v2c -c public -Oqv 192.168.0.33' \
' sysName'\
...
1
vote
2answers
54 views
Splitting the working directory in a bash script
If a do:
IFS="/" read -ra PARTS
And type in a path manually, it creates the array "PARTS" as hoped, however:
IFS="/" read -ra PARTS <<< $(pwd)
creates an array with a single element, ...
3
votes
3answers
114 views
Bash : Native way to get rid of quotation around each array member
I read an array from another script. This array needs to put " " around all array members since some members are empty.
in_file=./data
vector=($(./readdata.sh 0 $in_file))
for index in ${!vector[@]}
...
0
votes
1answer
36 views
how to count the length of an array defined in bash?
I'm new to bash and can't find a good tutorial to answer my question.
array=( item1 item2 item3)
for name in ${array[@]}; do
echo current/total
... some other codes
done
I want to calculate ...
1
vote
2answers
33 views
Read file remove spaces and store in array
I have a file with the following content:
list:
blue,none
red,none
orange,plot
baseball , none
university,none
school,none
desk,plot
monitor,none
earphone,none
I need to read this file, ...
1
vote
3answers
47 views
access elements in string array - stray @ symbol
I have created an array (3 elements), and each element contains a comma delimited string.
The array was created by reading a file line by line - the file contains fields output from a database.
I have ...
2
votes
2answers
73 views
Create array in bash with variables as array name
I'm not sure if this has been answered, I've looked and haven't found anything that looks like what I'm trying to do.
I have a number of shell scripts that are capable of running against a ksh or ...
2
votes
1answer
47 views
Push onto array with find exec
I want to iterate over all the files found by find and add each one to an array. Here's what I have so far:
myarray=()
find . -name '*.php' -exec myarray\+=\({}\) \;
echo "${myarray[@]}"
Instead, ...
4
votes
1answer
208 views
Bash Shell Script Array Length Off By One
The length of an array in my bash shell script appears to be off by one. I have 11 elements in a text file that I am reading in to an array, but my array length seems to be 12.
(( count = 0 ))
while ...
1
vote
1answer
57 views
Processing output from an sqlite db into a ksh array with spaces
I am querying an SQLite3 database like so:
input=$(-separator "," "SELECT field1,field2,field3 FROM table1")
and get this result:
Red,Yellow is a color,Blue
I need to insert this into an array, ...
3
votes
1answer
61 views
How to sort an associative array and retain the keys?
I have an array with filenames as keys and a numerical value as values.
MYARRAY[00001.jpg] = 31
MYARRAY[00002.jpg] = 200
MYARRAY[00003.jpg] = 98
I need to sort them so they are ordered by value.
...
0
votes
2answers
62 views
Multiple variable expansion modifiers in the same expression
Why does the following idiom not work in bash 4.1.0?
if [[ "${FUNCNAME[*]:1/$FUNCNAME/}" != "${FUNCNAME[*]:1}" ]]
Here it is in context...
function isCircularRef_test () {
#
### Seems like ...
-1
votes
2answers
34 views
if else statement
I have a case statement inside some of the if statement, but after doing the case statement inside the if statement, the loop ends. After the case statement, it is supposed to go to the next value on ...
1
vote
1answer
43 views
Whats wrong in creating/printing this array?
To find the average time taken to create certain files, I'm using this minutes array, then would simply use bash arithmetic to find the average. However I'm unable to get the difference except for the ...
2
votes
3answers
199 views
Loop over associative arrays by substring
In the following code, I create some associative arrays in a loop. The consist of two strings, a string identifier and a year. After creation, I want to access the arrays in a loop based on the ...
2
votes
1answer
22 views
CInt Configure Script: Syntax error at left parenthesis
I am trying to install CInt. When running ./configure, I get the following error:
./configure: 23: ./configure: Syntax error: "(" unexpected
Here is the relevant section of configure:
# configure ...
0
votes
1answer
121 views
Bash script: array elements containing space character [duplicate]
I'm getting started with bash scripting. I'm working with array elements that contain space characters. In the code below, the third array element is "Accessory Engine". I tried to set the space ...
1
vote
1answer
101 views
Why does the printf statement in this loop output the array out of sequence?
When I execute the following code, the output of the printf statement appears to show array out of sequence. In the declare statement the Source item is declared before Destination and in the output ...
0
votes
2answers
91 views
Delete last character of last item in a bash array
I have such array:
Array={123},{456}
Now I want to delete the last item 6.
1
vote
1answer
177 views
Compare two Arrays in KSH and output the difference
I am not extremely familiar with KSH (Actually just started using it) and I am having problems with trying to create a script that will essentially compare two arrays that have been stored and then ...
0
votes
1answer
47 views
Insert variable value as element in array
If I have a variable that has a value stored and I want to place that value into an array using the name of the variable, how would I do this?
e.g.:
variable="Hello there"
array[0]=$variable
does ...
1
vote
3answers
57 views
Use a variable as part of name of an array name?
I essentially have a for loop where the variable i that I am iterating with will take on each letter of the alphabet, for example. I want to use each value of i to create an array called "$i"array ...
0
votes
1answer
36 views
For value falls within a range get corresponding value
I am thinking to use awk to search a value from input file in a reference file and get a corresponding value from reference file.
Both reference and input files can be sorted based on $3 and $2 ...
2
votes
2answers
326 views
parse one field from an JSON array into bash array
I have a JSON output that contains a list of objects stored in a variable. (I may not be phrasing that right)
[
{
"item1": "value1",
"item2": "value2",
"sub items": [
{
...
1
vote
3answers
252 views
How do I test if an item is in a bash array?
Help for a simple script
#!/bin/bash
array1=(
prova1
prova2
slack64
)
a="slack64"
b="ab"
if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
else
echo "$a = $b: a is not equal to b"
...
2
votes
2answers
159 views
Create arrays with brace expansion in loop
I would like to generate a number of arrays that differ only by year. In the loop, I create the arrays with brace expansion and a variable.
I tried the following code without success:
...
2
votes
0answers
43 views
Generate array of strings combined with number
I would like to generate a local array in a shell/bash script that consists of the following elements: JF-1998, JF-1999, JF-2000,... , JF-2011. That is, a string JF- combined with the years from 1998 ...
1
vote
1answer
93 views
awk array using number as value did not work
I have a reference file:
Refrence File
Dpse\GA30012 FBgn0000447 chr2 26607738 26607962 -1
Dpse\GA19764 FBgn0085819 chrX 28571020 28571736 -1
Dpse\ttk FBgn0000100 chr2 ...
0
votes
2answers
561 views
How can I store each seperate entire line in a text file into an array?
I have a file called "threewords". It contains the information:
\#gray speedy bee
gr-A | sp-E-d-E | b-E
\#gray greedy pea
gr-A | gr-E-d-E | p-E
When I run the command:
cat threewords | grep ^# ...
0
votes
2answers
62 views
for loop not working for multiple lines
I have a array like this "Apple Banana Clementine Date"
I have to print like this:
1. Apple
2. Banana
3. Clementine
4. Date
Script file:
for i in "${fruits[@]}"; do
echo "$lineno. $i "
...
0
votes
0answers
39 views
awk - how does the for loop work when counting words? [duplicate]
I have got the following awk script from here that counts the frequency of the words in a file-:
# Print list of word frequencies
{
for (i = 1; i <= NF; i++)
freq[$i]++
}
END {
...
2
votes
1answer
55 views
Bash Array Comparision
Within a bash script, I am building two dynamic arrays. And I want to construct another array with difference of two.
Example:
array1=("aa-1234|safsf|sfsfs" "aa-2345|sfsfs|n0sfm" ...
3
votes
3answers
226 views
Split in awk not not printing array values
I have a string tstArr2 which has the following content
'3 5 8'
Now in awk I want to parse a flat file
test my array which array is better array
INDIA USA SA NZ AUS ARG GER BRA
US AUS INDIA ENG ...
1
vote
0answers
59 views
How to force bash to start at index 0 with select output?
Well, even some less-experienced bash users would know meanwhile about the fact that bash arrays would always start with index 0.
However, select will always do its own thing, as the following example ...
1
vote
2answers
63 views
Randomize arguments ( that hold color codes) from an array
I have the array (shown below) with arguments that hold color codes.
The intended purpose of my script is to randomize those arguments when the user inputs "scriptname randoms Text" so that every time ...
1
vote
5answers
465 views
How to create an array of unique elements from a string/array in bash?
If I have a string "1 2 3 2 1" - or an array [1,2,3,2,1] - how can I select the unique values, i.e.
"1 2 3 2 1" produces "1 2 3"
or
[1,2,3,2,1] produces [1,2,3]
Similar to uniq but uniq seems ...
2
votes
2answers
174 views
How to select unique array values?
I have an array and I want to get the unique 2nd members
bash doesn't really have 2-dimensional array so I've defined it this way, using :: as the separator of the two elements:
ruby_versions=(
...
5
votes
1answer
788 views
Provide two arguments to one option using getopts
In below code when I give option r then getopts requires one arguments:
while getopts ":hr::l:" opt; do
case $opt in
r ) echo "Run Numbers - argument = $OPTARG " ;;
l ) echo ...
0
votes
1answer
229 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
197 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:
...
1
vote
1answer
83 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
60 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
625 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
240 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
114 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
113 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() {
...
1
vote
3answers
840 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
2answers
7k 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 ...