我试图存储以下sql select语句中的每个值,并使用bash将它们存储在单独的变量中。
#!/bin/bash
mysqlhost="thehost"
mysqldb="thedb"
mysqlun="theusername"
mysqlpw="thepassword"
mysqlconnection="--disable-column-names --host=$mysqlhost --user $mysqlun --password=$mysqlpw --database=$mysqldb"
declare -a pinIDs=$(mysql $mysqlconnection -e "SELECT pinID FROM somewhere WHERE something = something";)
使用代码时得到以下结果
echo $pinIDs
8 11 23 26
我需要将每个值存储到它们自己的变量中。
最佳答案
添加方括号以将输出放入数组pinIDs
。更换
declare -a pinIDs=$(mysql $mysqlconnection -e "SELECT pinID FROM somewhere WHERE something = something";)
通过
declare -a pinIDs=( $(mysql $mysqlconnection -e "SELECT pinID FROM somewhere WHERE something = something";) )
然后查看以下内容的输出:
declare -p pinIDs