I am trying to store MySQL result into a global bash array variable but I don't know how to do it.
我正在尝试将MySQL结果存储到一个全局bash数组变量中,但是我不知道怎么做。
Should I save the MySQL command result in a file and read the file line by line in my for
loop for my other treatment?
我应该将MySQL命令保存到一个文件中,并在for循环中逐行读取文件,以便进行其他处理吗?
Example:
例子:
#Database, table uses
user password
Pierre aaa
Paul bbb
Command:
命令:
$results = $( mysql –uroot –ppwd –se « SELECT * from users );
I want that results
contains the two rows.
我希望结果包含这两行。
2 个解决方案
#1
4
Mapfile for containing whole table into one bash variable
You could try this:
你可以试试这个:
mapfile result < <(mysql –uroot –ppwd –se "SELECT * from users;")
Than
比
echo ${result[0]%$'\t'*}
echo ${result[0]#*$'\t'}
or
或
for row in "${result[@]}";do
echo Name: ${row%$'\t'*} pass: ${row#*$'\t'}
done
Nota This will work fine while there is only 2 fields by row. More is possible but become tricky
当一行只有两个字段时,这将会正常工作。更多是可能的,但会变得棘手
Read for reading table row by row
while IFS=$'\t' read name pass ;do
echo name:$name pass:$pass
done < <(mysql -uroot –ppwd –se "SELECT * from users;")
Read and loop to hold whole table into many variables:
i=0
while IFS=$'\t' read name[i] pass[i++];do
:;done < <(mysql -uroot –ppwd –se "SELECT * from users;")
echo ${name[0]} ${pass[0]}
echo ${name[1]} ${pass[1]}
#2
0
-
If you're looking to get a global variable inside your script you can simply assign a value to a varname:
如果您希望在脚本中获得全局变量,可以简单地为varname分配一个值:
VARNAME=('var' 'name') # no space between the variable name and value
Doing this you'll be able to access VARNAME's value anywhere in your script after you initialize it.
这样,在初始化VARNAME后,您就可以在脚本中的任何地方访问它的值。
-
If you want your variable to be shared between multiple scripts you have to use export:
如果想要在多个脚本之间共享变量,则必须使用导出:
script1.sh:
script1.sh:
export VARNAME=('var' 'name') echo ${VARNAME[0]} # will echo 'var'
script2.sh
script2.sh
echo ${VARNAME[1]} # will echo 'name', provided that # script1.sh was executed prior to this one
NOTE that export will work only when running scripts in the same shell instance. If you want it to work cross-instance you would have to put the export variable code somewhere in .bashrc or .bash_profile
注意,导出仅在在同一shell实例中运行脚本时才会工作。如果您想让它在跨实例中工作,您必须将导出变量代码放在.bashrc或.bash_profile中的某个位置
#1
4
Mapfile for containing whole table into one bash variable
You could try this:
你可以试试这个:
mapfile result < <(mysql –uroot –ppwd –se "SELECT * from users;")
Than
比
echo ${result[0]%$'\t'*}
echo ${result[0]#*$'\t'}
or
或
for row in "${result[@]}";do
echo Name: ${row%$'\t'*} pass: ${row#*$'\t'}
done
Nota This will work fine while there is only 2 fields by row. More is possible but become tricky
当一行只有两个字段时,这将会正常工作。更多是可能的,但会变得棘手
Read for reading table row by row
while IFS=$'\t' read name pass ;do
echo name:$name pass:$pass
done < <(mysql -uroot –ppwd –se "SELECT * from users;")
Read and loop to hold whole table into many variables:
i=0
while IFS=$'\t' read name[i] pass[i++];do
:;done < <(mysql -uroot –ppwd –se "SELECT * from users;")
echo ${name[0]} ${pass[0]}
echo ${name[1]} ${pass[1]}
#2
0
-
If you're looking to get a global variable inside your script you can simply assign a value to a varname:
如果您希望在脚本中获得全局变量,可以简单地为varname分配一个值:
VARNAME=('var' 'name') # no space between the variable name and value
Doing this you'll be able to access VARNAME's value anywhere in your script after you initialize it.
这样,在初始化VARNAME后,您就可以在脚本中的任何地方访问它的值。
-
If you want your variable to be shared between multiple scripts you have to use export:
如果想要在多个脚本之间共享变量,则必须使用导出:
script1.sh:
script1.sh:
export VARNAME=('var' 'name') echo ${VARNAME[0]} # will echo 'var'
script2.sh
script2.sh
echo ${VARNAME[1]} # will echo 'name', provided that # script1.sh was executed prior to this one
NOTE that export will work only when running scripts in the same shell instance. If you want it to work cross-instance you would have to put the export variable code somewhere in .bashrc or .bash_profile
注意,导出仅在在同一shell实例中运行脚本时才会工作。如果您想让它在跨实例中工作,您必须将导出变量代码放在.bashrc或.bash_profile中的某个位置