Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect output to a bash array

I have a file containing the string

ipAddress=10.78.90.137;10.78.90.149

I'd like to place these two IP addresses in a bash array. To achieve that I tried the following:

n=$(grep -i ipaddress /opt/ipfile |  cut -d'=' -f2 | tr ';' ' ')

This results in extracting the values alright but for some reason the size of the array is returned as 1 and I notice that both the values are identified as the first element in the array. That is

echo ${n[0]}

returns

10.78.90.137 10.78.90.149

How do I fix this?

Thanks for the help!

like image 779
calvinkrishy Avatar asked Nov 18 '09 02:11

calvinkrishy


People also ask

How do you store output in an array?

If you need to store it long-term, output it to a file instead of to a container in RAM. (Look up “open”, “close”, and “fprintf” in your Gnu C Standard Library Manual. (If you don't have one, get one; it's free; Google for it.)) to store anything in an array you need its index.

How do you store the output of a command to an array in shell script?

Explanation: $IFS=$'\n' : $IFS is bash's input field separator, setting it to the newline character only ( \n ) ensures that your output lines won't be split on whitespace so that you can save each line as a separate array element. Without this, each word of your command's output would be a different element.

How do I redirect a output to a file in bash?

For utilizing the redirection of bash, execute any script, then define the > or >> operator followed by the file path to which the output should be redirected. “>>” operator is used for utilizing the command's output to a file, including the output to the file's current contents.

How do I echo an array in bash?

How to Echo a Bash Array? To echo an array, use the format echo ${Array[0]}. Array is your array name, and 0 is the index or the key if you are echoing an associative array. You can also use @ or * symbols instead of an index to print the entire array.


2 Answers

do you really need an array

bash

$ ipAddress="10.78.90.137;10.78.90.149"
$ IFS=";"
$ set -- $ipAddress
$ echo $1
10.78.90.137
$ echo $2
10.78.90.149
$ unset IFS
$ echo $@ #this is "array"

if you want to put into array

$ a=( $@ )
$ echo ${a[0]}
10.78.90.137
$ echo ${a[1]}
10.78.90.149

@OP, regarding your method: set your IFS to a space

$ IFS=" "
$ n=( $(grep -i ipaddress file |  cut -d'=' -f2 | tr ';' ' ' | sed 's/"//g' ) )
$ echo ${n[1]}
10.78.90.149
$ echo ${n[0]}
10.78.90.137
$ unset IFS

Also, there is no need to use so many tools. you can just use awk, or simply the bash shell

#!/bin/bash
declare -a arr
while IFS="=" read -r caption addresses
do
 case "$caption" in 
    ipAddress*)
        addresses=${addresses//[\"]/}
        arr=( ${arr[@]} ${addresses//;/ } )
 esac
done < "file"
echo ${arr[@]}

output

$ more file
foo
bar
ipAddress="10.78.91.138;10.78.90.150;10.77.1.101"
foo1
ipAddress="10.78.90.137;10.78.90.149"
bar1

$./shell.sh
10.78.91.138 10.78.90.150 10.77.1.101 10.78.90.137 10.78.90.149

gawk

$ n=( $(gawk -F"=" '/ipAddress/{gsub(/\"/,"",$2);gsub(/;/," ",$2) ;printf $2" "}' file) )
$ echo ${n[@]}
10.78.91.138 10.78.90.150 10.77.1.101 10.78.90.137 10.78.90.149
like image 125
ghostdog74 Avatar answered Nov 16 '22 01:11

ghostdog74


This one works:

n=(`grep -i ipaddress filename | cut -d"=" -f2 | tr ';' ' '`)

EDIT: (improved, nestable version as per Dennis)

n=($(grep -i ipaddress filename | cut -d"=" -f2 | tr ';' ' '))
like image 33
Joy Dutta Avatar answered Nov 16 '22 01:11

Joy Dutta