#!/bin/bash
# 定义数据库连接信息
HOSTNAME="localhost"
DATABASE="test"
USERNAME="root"
PASSWORD="MyPassWord"
TABLE="users"
# 使用mysql命令查询数据并获取所有的id
ids=$(echo "SELECT Id FROM $TABLE;" | mysql -h$HOSTNAME -u$USERNAME -p$PASSWORD $DATABASE | tail -n +2)
# 对每个id进行循环处理
for id in $ids; do
# 查询Name并输出到文件
echo "SELECT Name FROM $TABLE WHERE Id = $id;" | mysql -h$HOSTNAME -u$USERNAME -p$PASSWORD $DATABASE | tail -n +2 >> result
.txt
# 使用tr命令将字母转换为大写并更新文件
# tr '[:lower:]' '[:upper:]' < result.txt > temp.txt
# mv temp.txt result.txt
# 将文件内容读入变量
# name=$(cat result.txt)
# 使用mysql命令更新数据
# echo "UPDATE $TABLE SET Name = '$name' WHERE Id = $id;" | mysql -h$HOSTNAME -u$USERNAME -p$PASSWORD $DATABASE
done