加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在shell脚本中将多个MySQL列提取到环境变量中?

发布时间:2020-12-15 21:04:00 所属栏目:安全 来源:网络整理
导读:我想从表中将一些数据提取到 shell脚本中的变量中.现在我做这样的事情: export NAME=`mysql -NB -e "select name from user where id = $ID"`export AGE=`mysql -NB -e "select age from user where id = $ID"`export LOCATION=`mysql -NB -e "select locat
我想从表中将一些数据提取到 shell脚本中的变量中.现在我做这样的事情:

export NAME=`mysql -NB -e "select name from user where id = $ID"`
export AGE=`mysql -NB -e "select age from user where id = $ID"`
export LOCATION=`mysql -NB -e "select location from user where id = $ID"`

这很好,但我喜欢在一个查询中完成所有操作,例如:

select name,age,location from user where id = $ID

我尝试使用“read”这样:

mysql -NB -e "select name,location from user where id = $ID" | read NAME AGE LOCATION
echo $NAME

…但它没有像我预期的那样工作(“echo”行没有输出).我错过了什么吗?

解决方法

如果使用read in pipe,它将创建一个子shell并在此子shell中设置一个不影响父shell(您工作的地方)的变量.
一个主意:

export IFS=. ; set -- $(mysql -N "select CONCAT_WS('.',name,location) from user where id=$ID")
NAME="$1" ; AGE="$2" ; LOCATION="$3"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读