0

I already have a shell command which creates lines like this one (from a long ss -an....|...|...):

5 10.1.1.20 3307  (Nb IP port)

I have an echo command to simulate the answer:

echo -e "5 10.1.1.20 3307\n25 10.1.1.30 3307\n5 10.1.1.20 3308" 

and I have an array :

declare -A dbArray
dbArray[3306]="mysql"
dbArray[3307]="mysql_test_db"
dbArray[3308]="mysql_other_db"

I would like to have the following command:

echo -e "5 10.1.1.20 3307\n25 10.1.1.30 3307\n5 10.1.1.20 3308" | do_something

to return

5 10.1.1.20 3307 mysql_test_db
25 10.1.1.30 3307 mysql_test_db
5 10.1.1.20 3308 mysql_other_db

by getting the port number from the input data (eg: 3307) and getting the content of the array with it (in this case, mysql_test_db).

I know how to replace the port number with something, and how to manage the array... But I can't answer the question on how to extract the port number to 'lookup' in the array (all this in the pipe).

I think that it is an interesting challenge, but too hard for me.

If someone can help...

I am on Redhat 7, using bash 4.2.46

Christian

1 Answer 1

0

do_something may be a bash-script like

#!/usr/bin/env bash

declare -A dbArray
dbArray[3306]="mysql"
dbArray[3307]="mysql_test_db"
dbArray[3308]="mysql_other_db"

while read -a args ; do
        echo ${args[0]} ${args[1]} ${args[2]} ${dbArray[${args[2]}]}
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .