将查询存储在bash中的数组中

时间:2022-07-04 05:59:35

My script need to store in a structure the result of a query:

我的脚本需要将查询结果存储在一个结构中:

 #!/bin/bash
 user="..."
 psw="..."
 database="..."
 query="select name, mail from t"
 customStructure=$(mysql -u$user -p$psw $database -e "$query";)

I've no idea how store the array of {name, mail} from query result..

我不知道如何从查询结果中存储{name, mail}的数组。

I need structure like this:

我需要这样的结构:

 array=[ [name1,mail1] , [name2,mail2], ....., [nameN, mailN] ]

Is there a way to do this in bash?

在bash中是否有这样的方法?

2 个解决方案

#1


16  

Bash arrays are initialized like so:

Bash数组初始化方式如下:

myarray=("hi" 1 "2");

To capture the individual portions of output of a command into an array, we must loop through the output, adding it's results to the array. That can be done like so:

要将命令输出的各个部分捕获到数组中,我们必须循环输出,将输出的结果添加到数组中。可以这样做:

for i in `echo "1 2 3 4"`
do
    myarray+=($i)
done

In your example, it looks like you wish to get the output of a MySQL command and store the parts of it's output lines into subarrays. I will show you how to capture lines into arrays, and given that, you should be able to figure out how to put subarrays into that yourself.

在您的示例中,看起来您希望获得MySQL命令的输出,并将其输出行的部分存储到子数组中。我将向您展示如何将行捕获到数组中,鉴于此,您应该能够自己找出如何将子数组放入数组中。

while read line
do 
    myarray+=("$line")
done < <(mysql -u${user} -p${psw} ${database} -e "${query}")

It's also worth mentioning that for this kind of MySQL operation, where you don't need output metadata (such as pretty formatting and table names), you can use MySQL's -B option to do 'batch output'.

值得一提的是,对于这种MySQL操作,您不需要输出元数据(比如漂亮的格式和表名),您可以使用MySQL的-B选项来执行“批处理输出”。

#2


3  

Field level record can be accessed via read -a command and IFS is set to the empty string to prevent read from stripping leading and trailing whitespace from the line.

可以通过read -a命令访问字段级记录,并将IFS设置为空字符串,以防止从行中删除前导和尾随空格。

#!/bin/bash
user="..."
psw="..."
database="..."
query="select name, mail from t"

OIFS="$IFS" ; IFS=$'\n' ; oset="$-" ; set -f

while IFS="$OIFS" read -a line 
do 
  echo ${line[0]}   
  echo ${line[1]}   
done < <(mysql  -u${user} -p${psw} ${database} -e "${query}")

#1


16  

Bash arrays are initialized like so:

Bash数组初始化方式如下:

myarray=("hi" 1 "2");

To capture the individual portions of output of a command into an array, we must loop through the output, adding it's results to the array. That can be done like so:

要将命令输出的各个部分捕获到数组中,我们必须循环输出,将输出的结果添加到数组中。可以这样做:

for i in `echo "1 2 3 4"`
do
    myarray+=($i)
done

In your example, it looks like you wish to get the output of a MySQL command and store the parts of it's output lines into subarrays. I will show you how to capture lines into arrays, and given that, you should be able to figure out how to put subarrays into that yourself.

在您的示例中,看起来您希望获得MySQL命令的输出,并将其输出行的部分存储到子数组中。我将向您展示如何将行捕获到数组中,鉴于此,您应该能够自己找出如何将子数组放入数组中。

while read line
do 
    myarray+=("$line")
done < <(mysql -u${user} -p${psw} ${database} -e "${query}")

It's also worth mentioning that for this kind of MySQL operation, where you don't need output metadata (such as pretty formatting and table names), you can use MySQL's -B option to do 'batch output'.

值得一提的是,对于这种MySQL操作,您不需要输出元数据(比如漂亮的格式和表名),您可以使用MySQL的-B选项来执行“批处理输出”。

#2


3  

Field level record can be accessed via read -a command and IFS is set to the empty string to prevent read from stripping leading and trailing whitespace from the line.

可以通过read -a命令访问字段级记录,并将IFS设置为空字符串,以防止从行中删除前导和尾随空格。

#!/bin/bash
user="..."
psw="..."
database="..."
query="select name, mail from t"

OIFS="$IFS" ; IFS=$'\n' ; oset="$-" ; set -f

while IFS="$OIFS" read -a line 
do 
  echo ${line[0]}   
  echo ${line[1]}   
done < <(mysql  -u${user} -p${psw} ${database} -e "${query}")