我在MYSQL表中有以下列:
ID START_TIME
51 2015-01-01 01:00:00
我在Bash中有以下MySQL查询:
Values=($(mysql -DHISTORIZATION -hlocalhost -uroot -pAdmin -se "SELECT ID, START_TIME FROM TABLE_NEW WHERE ID=51"));
当我尝试分配和打印这样的值时:
ID=Values[0];
START_TIME=Values[1];
echo $ID;
echo $ START_TIME;
打印方式如下:
51
2015-01-01
理想情况下,它应该打印出来,这是所需的输出:
51
2015-01-01 01:00:00
我打印时
echo ${Values[*]};
打印:
51 2015-01-01 01:00:00
值包含三个元素,但应包含两个元素。 我认为它的问题是由于" 2015-01-01 01:00:00"中的空格字符,但是如何摆脱这个?如果不改变IFS,我会很感激。
谢谢..
答案 0 :(得分:0)
mysql输出是制表符分隔的。
因此,这应该工作:
IFS=$'\t' read -a Values < <(mysql -DHISTORIZATION -hlocalhost -uroot -pAdmin -se "SELECT ID, START_TIME FROM TABLE_NEW WHERE ID=51");
答案 1 :(得分:0)
你可以使用连接日期和变量的变量时间。
echo ${value[*]};
2015-04-28 13:30:00
START_TIME=${value[0]}" "${value[1]}
我建议在mysql参数中使用-N,以便跳过列名。
答案 2 :(得分:0)
这应该有效:
IFS=$'\t'
Values=($(mysql -DHISTORIZATION -hlocalhost -uroot -pAdmin -se "SELECT ID, START_TIME FROM TABLE_NEW WHERE ID=51"))
ID=${Values[0]}
START_TIME=${Values[1]}
echo "$ID"
echo "$START_TIME"
答案 3 :(得分:0)
这是在bash,busybox,dash和ksh下进行测试的!
#!/bin/sh
user=root
pass=Admin
host=localhost
db=HISTORIZATION
fmreq="SELECT ID, UNIX_TIMESTAMP(START_TIME) as START_TIME
FROM TABLE_NEW WHERE ID=%d;"
req="`printf "$fmreq" $@`"
mysql -B -D$db -u$user -p$pass -h$host --column-names -se "$req" | (
tab=`printf "\t"`
read ans
ID="${ans%%$tab*}"
START_TIME="${ans#*$tab}"
date=`date -d @$START_TIME +"%F %T"`
printf "Id : %d\nStart: %s\n" $ID "$date"
)
$IFS
(但bash)#!/bin/bash
user=root pass=Admin host=localhost db=HISTORIZATION
fmreq="SELECT ID, START_TIME FROM TABLE_NEW WHERE ID=%d;"
[ -f .mysqlcred ] && . .mysqlcred
printf -v req "$fmreq" $@
read -r ans < <(
mysql -B -D$db -u$user -p$pass -se "$req"
)
array=()
while [ "$ans" != "$last" ];do
last="${ans%%$'\t'*}"
ans="${ans#*$'\t'}"
array+=("$last")
done
printf "Id : %d\nStart: %(%F %T)T\n" "${array[@]}"
declare -p array
可以给出:
./myscript 51
Id : 51
Start: 2015-01-01 01:00:00
declare -a array='([0]="51" [1]="1420070400")'
#!/bin/bash
user=root pass=Admin host=localhost db=HISTORIZATION
fmreq="SELECT ID, START_TIME FROM TABLE_NEW WHERE ID=%d;"
[ -f .mysqlcred ] && . .mysqlcred
printf -v req "$fmreq" $@
declare -A answer
{
read -r tit
read -r ans
while [ "$ans" != "$last" ];do
last="${ans%%$'\t'*}"
answer[${tit%%$'\t'*}]="$last"
ans="${ans#*$'\t'}"
tit="${tit#*$'\t'}"
done
} < <(
mysql -D$db -u$user -p$pass --column-names -re "$req"
)
printf "Id : %d\nStart: %(%F %T)T\n" ${answer[ID]} ${answer[START_TIME]}
declare -p answer
试验:
./myscript 51
Id : 51
Start: 2015-01-01 01:00:00
declare -A answer='([START_TIME]="1420070400" [ID]="51" )'
#!/bin/bash
user=root pass=Admin host=localhost db=HISTORIZATION
fmreq="SELECT ID, START_TIME FROM TABLE_NEW WHERE ID=%d;"
[ -f .mysqlcred ] && . .mysqlcred
printf -v req "$fmreq" $@
IFS=$'\t' read -a ans < <(
mysql -B -D$db -u$user -p$pass -se "$req"
)
printf "Id : %d\nStart: %(%F %T)T\n" ${ans[@]}
请注意,以这种方式更改 IFS 不会改变您脚本的其余部分。
Id : 0
Start: 2015-01-01 01:00:00
#!/bin/bash
user=root pass=Admin host=localhost db=HISTORIZATION
fmreq="SELECT ID, UNIX_TIMESTAMP(START_TIME) as START_TIME
FROM TABLE_NEW WHERE ID=%d;"
[ -f .mysqlcred ] && . .mysqlcred
printf -v req "$fmreq" $@
declare -A answer
{
read -r tit
read -r ans
while [ "$ans" != "$last" ];do
last="${ans%%$'\t'*}"
answer[${tit%%$'\t'*}]="$last"
ans="${ans#*$'\t'}"
tit="${tit#*$'\t'}"
done
} < <(
mysql -D$db -u$user -p$pass --column-names -re "$req"
)
printf "Id : %d\nStart: %(%F %T)T\n" ${answer[ID]} ${answer[START_TIME]}
declare -p answer
这可能会产生类似:
./myscript 51
Id : 51
Start: 2015-01-01 01:00:00
declare -A answer='([START_TIME]="1420070400" [ID]="51" )'