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

learn more… | top users | synonyms

-2
votes
0answers
23 views

Script bash with array working as root but not with sudo [on hold]

I'm on Ubuntu 16.04 LTS and when I execute a script with an array, I have errors : #/!/bin/bash array[0]="/" array[1]="/home/dev" echo ${array[@]} Ouput: dev@dev:~$ sudo ./script ./script: 3: ./...
0
votes
0answers
11 views

Dereferencing array [duplicate]

I have an array: $ arr=(one two) I want to access it's content through another variable which contains the name of the array: $ arrname=arr I want to access (one two) using the variable arrname. I ...
0
votes
1answer
28 views

How to loop through multiple arrays / using nested variable substitutions [duplicate]

I would like to create a series of arrays, each with 3 parameters, and then loop through each array calling each parameter at different times. I'm having trouble just getting the for loop to output ...
1
vote
2answers
31 views

Counting an array has twice the expected number of elements

Why does bash appear to count the words in an array rather than the number of elements? touch '1 red' '2 orange' '3 yellow' # Three filenames, each of two words files=( * ) ...
2
votes
1answer
23 views

How to find files based on array

I have an array that contains a list of filenames (without extension) ex: filearray=("fileA" "fileB" "fileC") These files are split amongst multiple directories. As I loop through the directories, ...
0
votes
1answer
32 views

Using variable to retrieve array element inside awk statement

Below is the part of the code that is throwing error. for (( p=0; p<${var2}; p++ )) do if [[ ${array2[$p]} == *"xyz"* ]] then awk -F" " '{ array3[p]="123" };1' ${array1[$i]} else awk -F" "...
0
votes
1answer
21 views

Reading decimals in text file into a 1D array [closed]

So I have this text file named sortedtime.txt containing sorted decimals and integers listed line by line: 12.6 13.7 ... 154 1864 I need a shell script that will read these numbers and add ...
1
vote
1answer
38 views

How to split Bash array into arguments

I wrote a bash script for listing python processes, ram usage and PID and status in human readable form with colored lines. But I have some trouble with working time of the script. Because of the ...
0
votes
0answers
13 views

Quitting Linux Screen does not stop loop

I'm running a loop of Mplayer commands (reading the paths from an array) in a detached screen: screen -S music -d -m sudo mp3_player.py The loop commands and output work perfectly and if the loop is ...
0
votes
0answers
22 views

unable to pipe the output of echo command to array [duplicate]

I am trying below bash-3.2$ echo 1 2 3 4 5 | read -a ARRAY1 bash-3.2$ echo ${ARRAY1[*]} bash-3.2$ echo $? 0 which I thought is to assign to the array ARRAY1 in single shot but when I tried to ...
2
votes
1answer
30 views

problem with the inactive RAID 5

I have a server running ubuntu server, after a crash, I lost a Disc. Here is the output: cat /proc/mdstat Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10] ...
1
vote
1answer
25 views

Adding to arrays [duplicate]

I have this script tests=() igrepy -l $1 . | while read -r line do // some processing tests+=("${second[0]}") done echo ${tests[@]} I've checked that 'second' has a result and it does, ...
1
vote
2answers
39 views

Raid array 'clean, degraded'?

Today I noticed that there are bunch of messages complaining about the RAID array (it's a software RAID10), so I started looking into it but need help because I'm unsure if I interpret the status ...
0
votes
1answer
31 views

Unique strings in bash [duplicate]

I have an array array=(src/ucode/pkgs/get_ch.c qa/tests/ucode/chktest.pl src/ucode/pkgs/get_ch.c src/profile/settings.txt src/ucode/pkgs/main_pf.c src/ucode/pkgs/get_ch.c src/ucode/pkgs/main_ch.c ...
2
votes
1answer
36 views

Array syntax with AWK

I am baffled at the response of these scripts. Trial 1: awk '{line_arr[$0] } END {for (line in line_arr) print line}' text_file Output: 1 2 3 4 Trial 2: awk '{line_arr[$0] } END {for (line in ...
0
votes
1answer
38 views

How to mark one of RAID1 disks as a spare? (mdadm)

I have a healthy and working software based RAID1 using 3 HDDs as active on my Debian machine. I want to mark one of the disks as a spare so it ends up being 2 active + 1 spare. Things like: mdadm -...
1
vote
1answer
80 views

Populate array with a sed/echo combination

I'm trying to rename multiple files containing dates. I want to split the date into year, month and day and then create a new filename in a directory tree like year/month-year/filename_yearmonthday....
1
vote
1answer
36 views

Suppress new line when Awk'ing an array

This is kind of a continuation of Awk and discarding non-matching tokens in a string? I am using Awk to filter unwanted compiler flags derived from CPU features (SunCC 12.2 cannot handle the same ...
-1
votes
1answer
51 views

Parsing a string using bash function

I'm trying to replace some codes with bash function to reuse it. So my previous codes are: #!/bin/bash set aline=" dsfsdf | fsdfff | yuhfd " set line_parsed=`echo $aline | tr '|' "\n"` echo ${...
1
vote
1answer
126 views

How to assign values of a matrix to variables in bash

I have created a 50 x 1 matrix in ubuntu as follows: #!/bin/bash declare -A matrix num_rows=1 num_columns=50 for ((n=0;n<=(($num_columns-1));n++)) do for ((i=1;i<=num_rows;i++)) do matrix[$...
-1
votes
1answer
117 views

How to write the bash script dynamic to print row and columns data and update the same data in the same file? [closed]

abc_hosts,pwd_host_id,pwd_host_id,hostname,ddd_status,dddd_status, start_hosts,,,,,,,,,,,,,,,,,,, ,1,o1,fhffhfh,1,1,fff,fdfd,172.33.33.33,172.30.30.12,172.30.30.11,oreere.dff,43,443343,1111,43435,...
1
vote
1answer
77 views

reading array and summation of elements

! /bin/bash I omitted here though I have used it. I used bash shell only. first integer is the no of variables in the array. yes I did get syntax error. I want to know the problem with my code. need ...
0
votes
2answers
92 views

Write ls to array (split by newline)

I want my directory listing output in an array. readarray x < <( ls -la ) printf "%s" ${x[@]} So I wrote this, which works but it splits by space and not by newline. I can't find a "newline" ...
1
vote
1answer
133 views

compare array in bash

Question: what does this line do? Can someone please explain? if [ $((${array[$i]}+1)) -ne ${array[$(($i + 1))]} ] ;then foo; fi
0
votes
1answer
40 views

Subtraction with Values in an Array

I'm using an array to try to subtraction. However, when my script just gives me the total of my array with a negative sign in front of it. Any suggestions? elif [ "$OP" = "Subtraction" ]; then ...
2
votes
1answer
90 views

Issue with array length in bash script

I am writing a script that stores some of the command line arguments as an array, and uses the array later, but I'm having an issues getting the correct length of the array in the script. In the ...
1
vote
0answers
65 views

removing spare from array using hpacucli

Whilst configuring a new array I have somehow accidentally added the same disk to 2 arrays: => ctrl all show config Smart Array P400 in Slot 1 (sn: PAFGK0P9SX036K) array A (SAS,...
0
votes
2answers
61 views

Define a set of values for a variable in shell script

I want to define a set of values for a single variable. E.g. singleDigit={0,1,2,3,4,5,6,7,8,9} If the user gives 10 it should exit with an error. How can this be achieved without using a loop, but ...
3
votes
1answer
76 views

Ordered by insertion Map in bash

Is there an ordered (by insertion) map in bash? I know there's an associative array data structure in bash, but when iterating over it, the order of the elements is not by insertion. Example below. ...
5
votes
2answers
162 views

Array Expansion

I am having unexpected results in expanding an array. I am hoping someone can shed some light on why I am seeing what I am seeing. I am trying to populate an array using fdisk -l to fill it and only ...
5
votes
4answers
108 views

Is there an easy way to do the equivalent of `sed …` replacing the same line with multiple values?

I have a file where I want to replace some variables with data from a shell script. -A INPUT -i lo -s @LOCAL_IP@ -j ACCEPT Here I want to replace @LOCAL_IP@ with an IP address, I use the following: ...
0
votes
1answer
53 views

parse snmpwalk output to array

Im trying to get network interfaces by doing a snmpwalk, but cannot separate in array, the variable stores all in element 0 ifname=(\$(snmpwalk -Oqv -v2c -c comm xxx.xxx.xxx.xxx 1.3.6.1.2.1.2.2.1.2))...
2
votes
2answers
103 views

Reading a file with IFS loop works only when no arrays are used

Using the following code, I'm trying to read a file into an array: GROUPS=() while IFS=: read -r g1 g2 g3 g4 do GROUPS+=("$g3") echo "$g3" done < /etc/group This doesn't work, it doesn't ...
2
votes
1answer
73 views

GNU Parallel: How can I reference array elements?

This is my case scenario: luis@Balanceador:~$ echo ${array[@]} a b luis@Balanceador:~$ echo ${array[1]} a luis@Balanceador:~$ echo ${array[2]} b luis@Balanceador:~$ parallel echo ${array[]} ::: 1 2 -...
1
vote
0answers
35 views

Returning an array from a ksh93 function

I would like to return an array from a function in ksh93. At this point, using the following method, the contents of the array are passed as a single element even though, in the fonction, the array ...
0
votes
0answers
46 views

How to resolve a variable's value inside an array in bash

I have a few files whose names I have assigned into variables. (There may be n number of files). Those variables are passed into an array. Now I want to print the names of the original files using ...
0
votes
3answers
110 views

Bash array values like variables inside loop

Im trying double loop using array values like array names for loop array1="name1 name2" name1="one two" name2="red blue" for name in $array1 do for value in $name do echo "$name - $value" ...
2
votes
2answers
44 views

Build a process substitution list for tee

I have a script like this which I use to broadcast commands to several instaces of the postgresql database CLI connected to several different servers. I'm using a hard-coded set of process-...
0
votes
2answers
37 views

How can I assign the last element of an array to a another array?

Let say I have an array $Info whose elements are ; ! total energy = -1090.13343774 Ry ! total energy = -1090.20757070 Ry ! total energy = -1090....
-4
votes
2answers
103 views

What is wrong in this code? [closed]

The code is ; cm=$1 nm=$2 case $cm in "out") declare -a en declare -a inf ec=$(grep -n "! " hw1_out_si_wire.txt) IFS=$'\n' en=($ec) lst=$((${#en[@]} -1)) IFS=' ' inf=($en[$lst]) echo " Energy: ${inf[...
0
votes
1answer
28 views

Why does the output look like different?

After executing this script, sometimes the output looks like this: 1912:! total e ergy = -1090.13343774 Ry 2310:! total e ergy = -1090.20757070 Ry 2709:! total e ...
1
vote
1answer
91 views

Array indexing using “sed”

I am working on one shell script which search directory modified in last 24 hours and then compress them using tar. Folder Search : find /path to log directory/ -maxdepth 1 -type d -mtime +0 ...
-1
votes
2answers
179 views

How to sum many numbers inside 2D array using awk

I need to sum numbers located in a file like this one: column1 column2 column3 row1 a(1,1) a(1,2) a(1,3) row2 a(2,1) a(2,2) a(2,3) row3 a(3,1) a(3,2) a(3,3) row4 a(4,1) ...
2
votes
1answer
79 views

How to sum numbers inside many 2D arrays

I need to sum numbers located in a file like this one: column1 column2 column3 row1 a(1,1) a(1,2) a(1,3) row2 a(2,1) a(2,2) a(2,3) row3 a(3,1) a(3,2) a(3,3) row4 a(4,1) a(4,2) ...
3
votes
2answers
42 views

Looking for a convenient way to create a “constant array”

In zsh, is there a convenient way to create an array of n copies of the same integer? Context: In a shell script, I need to create an array, tile_lengths, consisting of two longish constant blocks. ...
0
votes
0answers
41 views

Array value won't get printed out

I have the below bash script, but rather that I get the command executed stored in cmd[1] it prints [1]: command not found. I executed the command with the following way: $ qsub -J 1-33 sicer-pbs.sh ...
2
votes
4answers
105 views

String Manipulation Shell Script [duplicate]

I am working on a UPS Monitoring project using NUT Server. My objective is to make a shell script which sends one command and in response receives the status and other parameters from the UPS. for ...
1
vote
1answer
66 views

bash script read array outside loop

This is my first attempt to write a bash script and I'm unable to read array outside for loop. What I'm trying to do is.. store name of all files from /MyDir in an array. Check if there is process ...
2
votes
2answers
68 views

Split words from `read` and store to array?

How can I take input from read, split the words up by spaces, and then put those words into an array? What I want is: $ read sentence this is a sentence $ echo $sentence[1] this $ echo $sentence[2] ...
2
votes
2answers
84 views

find duplicate in first, second columns and sum numbers in third column

I have this input: Fred apples 20 Susy oranges 5 Mark watermellons 12 Robert pears 4 Robert oranges 17 Terry oranges 9 Lisa peaches 7 Susy oranges 12 Mark grapes 39 Anne mangoes 7 Greg pineapples 3 ...