shell 获取MySQL查询结果并处理

主要应用到shell for循环

定义数据库连接信息

HOST_NAME='127.0.0.1'
DB_PORT='3306'
DB_NAME='数据库名'
USER_NAME='root'
PASSWD='root'

TIME 当前时间戳 $() 注意date中间是有空格的

TIME=$(date '+%s')

-s 去掉表头

MYSQL_ETL="mysql -h${HOST_NAME} -P${DB_PORT} -u${USER_NAME} -p${PASSWD} ${DB_NAME} -s -e"
hive_table_sql="select user_id from mx_user where token_time >0 and online=1 and token_time <= ${TIME}"
hive_table=$($MYSQL_ETL "${hive_table_sql}")

for 变量 in 查出的数据 ,然后遍历这个变量 做 处理

for userid in $hive_table
do
此处逻辑处理(我这里的例子是查出再修改)
update_sql="update mx_user set online=0 where user_id=${userid}"
$($MYSQL_ETL "${update_sql}")
done

我当时在Windows编写的脚本,然后放到Linux里面直接执行会报错,原因是windows 的回车空格与linux的空格格式是不一样的,方法很多也很简单,借助工具submit_text编辑器里view > line engings 将Windows修改为Uinx保存即可!方法很多,这里一种可作参考,其他方法再自己百度。

https://www.cnblogs.com/lvtiansong/p/11755763.html

原文地址:https://www.cnblogs.com/seasonzone/p/14189834.html