Bash: Find and echo value in Array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: Find and echo value in Array
# 1  
Old 05-14-2011
Bash: Find and echo value in Array

Newbie to bash here. I think this is fairly simple, but I have searched and cannot figure it out. In the code below, I am searching an array for an IP address, and then printing the IP address if found. However, I would like to print the actual variable found such as 2.2.2.2=2, but cannot figure out how to find the actual location of the found variable in the array. Any help appreciated!

Code:
#!/bin/bash

IPs=("1.1.1.1=1" "2.2.2.2=2" "3.3.3.3=3")

IPAddr="2.2.2.2"

  if [[ ${IPs[@]} =~ $IPAddr ]];

        then
        echo "FOUND: $IPAddr"

  fi

# 2  
Old 05-14-2011
Something like this?
Code:
#!/bin/bash

IPs=( "1.1.1.1=1" "2.2.2.2=2" "3.3.3.3=3" )

IPAddr="2.2.2.2"

for i in ${IPs[@]}
do
  if [[ $i =~ $IPAddr ]]
  then
    echo $i
  fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Array connectin to another Array

Hello, i have a script that i need account_number to match a name. for exsample : ACCOUNT_ID=(IatHG8DC7mZbdymSoOr11w KbnlG2j-KRQ0-1_Xk356s8) and i run a loop curl requst with this the issue is that i want to know on which account were talking about so bash will know this : ... (4 Replies)
Discussion started by: batchenr
4 Replies

2. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

3. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

4. Shell Programming and Scripting

Basic bash, echo in loop for

Hi, I am trying to make a script to manage log. I want to write the name of the .gz I moved and the date : for i in `ls $replog/*.gz` do echo " $i " `echo $i date +%d:%m:%Y` `echo $datee `>> $replog/mrnet.log mv $i /var/log/vieux-logs done I need to echo... (10 Replies)
Discussion started by: Dabless
10 Replies

5. Shell Programming and Scripting

echo in bash

Why does echo supress line breaks in bash? I'm working on a script that starts like this: words=`sort list.txt | uniq` echo $words | wc -l I need to number the lines and then do other stuff. I'd use jot, but it's not installed, so I hope I can get seq to do what I want. But first I need to... (2 Replies)
Discussion started by: mregine
2 Replies

6. Shell Programming and Scripting

problem in bash echo

I am using the echo command to send the output to the file. I am using the following code: echo "service started successfully\n" > log But when I do: cat log I get: service started successfully\n Instead of a newline after the "successfully" Why is that and how can I fix it? (3 Replies)
Discussion started by: programAngel
3 Replies

7. Shell Programming and Scripting

Constant update echo in BASH

Hi all, Basically Im trying to put the current time in a script in BASH. Tried the watch command, but its not really what I want. I will have lots of things in this script, current date and time being just a few). Any ideas? (4 Replies)
Discussion started by: mikejreading
4 Replies

8. UNIX for Advanced & Expert Users

echo in ksh sh & bash

Hello, I have lib file which contain a function that get text to print on screen by echo command. Several scripts are inculde this lib and use this function. Each one of them is written in different shell language (sh ksh & bash). This causing some issues when using backslash charater as... (4 Replies)
Discussion started by: Alalush
4 Replies

9. Shell Programming and Scripting

create array holding characters from sring then echo array.

Hi, I wish to store $string1 in $string1array a character in each array element. Then i wish to echo the entire array to the screen so that it reads as the normal string again. I have been trying with the code below but does not work. Please help... To put string into array: ... (5 Replies)
Discussion started by: rorey_breaker
5 Replies

10. Shell Programming and Scripting

what is ksh equivalent of bash echo -n ?

Hi folks, I need to stop printing a new line after echoing a string in KSH. i know bash provides echo -n "string" what is the ksh equivalent for this ? (3 Replies)
Discussion started by: mudhireddy
3 Replies
Login or Register to Ask a Question