1

How could I separate the values ​​obtained in an array according to the search string? Example: I use this code:

#!/bin/bash
#Ref =  https://www.linuxquestions.org/questions/programming-9/bash-to-loop-thorouh-mysql-select-array-882907/#post5798608#
#
DBUSER="user"
DBNAME="pass"
results=($(mysql --user ${DBUSER}  ${DBNAME} -Bse "select phonenumber, userattr from usersSis WHERE sendMSG = '0';"))
cnt=${#results[@]}

for (( i=0; i<${cnt}; i++ ))
do
    echo "Total. $i: ${results[$i]} "

#    NumPhone1=${results[0]};
#    CodPhone1=${results[1]};
#    NumPhone2=${results[2]};
#    CodPhone2=${results[3]};

echo "---------------------------------------------------------------------------------------------------"
done

The result is :

Total. 0: 1223121219
---------------------------------------------------------------------------------------------------
Total. 1: 667
---------------------------------------------------------------------------------------------------
Total. 2: 3223121219
---------------------------------------------------------------------------------------------------
Total. 3: 2005
---------------------------------------------------------------------------------------------------
Total. 4: 5223121219
---------------------------------------------------------------------------------------------------
Total. 5: 545454
---------------------------------------------------------------------------------------------------

Could they look like this? REF: Loop through an array of strings in Bash? Creating an associative array. A dictionary:

declare -A continent

continent[Vietnam]=Asia
continent[France]=Europe
continent[Argentina]=America

for item in "${!continent[@]}"; 
  do
    printf "$item is in ${continent[$item]} \n"
  done
Output:

 Argentina is in America
 Vietnam is in Asia
 France is in Europe

var1 = 1223121219 and var2 = 667 var3 = ... and var4 = ... ... ...

banana
  • 31
  • 2
  • 10
  • Not clear, please do mention your expected sample output more clearly in your question and let us know then. – RavinderSingh13 Jan 21 '20 at 07:13
  • @banana: I don't see how an associative array would help you here. Of course you can do something like `data[phonenumber]=${result[0]}`, but you want to store many data pairs. In a higher programming language, you would store them as array of records, each record having a field for phone number and user attribute. You don't have this in bash; it's simply the wrong tool for this kind of task. – user1934428 Jan 21 '20 at 07:19
  • Show a line of output from `mysql -Bse "select phonenumber, userattr from usersSis WHERE sendMSG = '0';"` – David C. Rankin Jan 21 '20 at 07:43

2 Answers2

0

If I understand your query result format being "phonenum userattr" on each line, then you can create an associative array keyed by userattr (e.g. result[$userattr]=phonenum). To separate the values into the associative array, you can use process substitution (instead of command substitution) to provide input to a while loop that you use to create the associative array, e.g.

#!/bin/bash
#Ref =  https://www.linuxquestions.org/questions/programming-9/bash-to-loop-thorouh-mysql-select-array-882907/#post5798608#
#
DBUSER="user"
DBNAME="pass"

declare -A result

while read -r phone userattr; do
    result[$userattr]=$phone
done < <(mysql --user ${DBUSER}  ${DBNAME} -Bse "select phonenumber, userattr from usersSis WHERE sendMSG = '0';")

for attrib in "${!result[@]}"; do
    printf "%-8s : %s\n" "$attrib" "${result[$attrib]}"
done

(note: your cnt=${#result[@]} would work fine above as well)

I don't have your database to test with, but with your values above as "phonenum userattr" per-line of input, the output would be:

545454   : 5223121219
2005     : 3223121219
667      : 1223121219

(note: be aware that associative arrays do you preserve the order the data is input, it is an associative array, the order the elements were added in is irrelevant)


Test Input Used

Given the difficulty you are having, a couple of troubleshooting steps and comparisons are warranted. With your post of the output, the output of your command:

mysql --user ${DBUSER}  ${DBNAME} -Bse "select phonenumber, userattr from usersSis WHERE sendMSG = '0';"

should be:

1223121219 667
3223121219 2005
5223121219 545454

So let's test with a simple input file. Save what is above to a file on your computer. I have it in sqlout, e.g.

$ cat sqlout
1223121219 667
3223121219 2005
5223121219 545454

Now change the script to read the file instead of the process substitution, e.g.

#!/bin/bash

declare -A result

while read -r phone userattr; do
    result[$userattr]=$phone
done < sqlout

echo "${#result[@]} entries"

for attrib in "${!result[@]}"; do
    printf "%-8s : %s\n" "$attrib" "${result[$attrib]}"
done

Then run the script and check that you are getting all output, e.g.

Example Use/Output

$ bash readmysql.sh
3 entries
545454   : 5223121219
2005     : 3223121219
667      : 1223121219

Let me know if you do not get the same output and we will go from there.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Show userattr, but phone not. You will do some tests and have a position. For now, thank you – banana Jan 22 '20 at 00:49
  • If you can post a line of the output from your `mysql` command, I'll update to make sure it works. (I just assumed a format `"1223121219 667"` per line) – David C. Rankin Jan 22 '20 at 00:53
  • mysql> select * from usersSis; +----+-------+-------------+----------+------------+---------+ | id | ipNow | phonenumber | userattr | dateExpire | sendMSG | +----+-------+-------------+----------+------------+---------+ | 1 | NULL | 1223121219 | 667 | NULL | 0 | | 2 | NULL | 3223121219 | 2005 | NULL | 0 | | 3 | NULL | 5223121219 | 545454 | NULL | 0 | +----+-------+-------------+----------+------------+---------+ 3 rows in set (0.00 sec) mysql> – banana Jan 22 '20 at 01:36
  • With that post, the result of your query in your question should produce lines with `"1223121219 667"` and `"3223121219 2005"` and `"5223121219 545454"` which is exactly what the code above preserves in the associative array. So in your first comment are you saying the only output you get is `"545454 : "` and no phonenumber? – David C. Rankin Jan 22 '20 at 01:42
  • Yes, only userattr code output. without phone number. – banana Jan 22 '20 at 01:47
  • Wow, that is really strange. There are a couple of reasons I can think of (1) you are on some OS, like windows where the default line-ending is `CRLF` (e.g. `"\r\n"`) -- but that should just cause the 2nd and 3rd line read problems, or (2) you have some strange delimiter between the two fields in your output that is causing problems. Are you on Linux, Mac or Windows? I'll drop an edit and show the test used. – David C. Rankin Jan 22 '20 at 01:52
  • @banana take a look at the edit and the use of a file to verify the input format from your mysql command. If you have additional problems, let's continue this discussion in chat. – David C. Rankin Jan 22 '20 at 01:59
  • Thank you for your help. Thank you very much. I was sick, but I will continue testing. Thank you brother! – banana Jan 28 '20 at 04:04
0
mysql> select * from usersSis;
+----+-------+-------------+----------+------------+---------+
| id | ipNow | phonenumber | userattr | dateExpire | sendMSG |
+----+-------+-------------+----------+------------+---------+
|  1 | NULL  | 1223121219  | 667      | NULL       | 0       |
|  2 | NULL  | 3223121219  | 2005     | NULL       | 0       |
|  3 | NULL  | 5223121219  | 545454   | NULL       | 0       |
+----+-------+-------------+----------+------------+---------+
3 rows in set (0.00 sec)

mysql>

Result of cod: root@us:/var/www/html/arquivos# ./script.sh 545454 : 2005 : 667 :

in script > You script example code =D

banana
  • 31
  • 2
  • 10
  • From the command line, outside mysql, show the output of `mysql --user ${DBUSER} ${DBNAME} -Bse "select phonenumber, userattr from usersSis WHERE sendMSG = '0';"` with the variables `DBUSER` and `DBNAME` replaced with any required user, or just eliminated altogether. You should get a 3-line output with only the `"phonenumber userattr"` per-line. – David C. Rankin Jan 22 '20 at 01:44