主要應用到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