背景:因项目需要,定期修改mysql两个查询用户的密码;
#!/bin/bash user=app1|app2 oldPwd=12345|12345 newPwd=45678|45678 if [[ $user == *"|"* ]] && [[ $oldPwd == *"|"* ]] && [[ $newPwd == *"|"* ]];then echo "user and pwd is ok" else echo "must have 管道符|分隔,please add" exit 3 fi #用户 user1=${user%|*} user2=${user#*} echo "第一个用户:$user1,第二个用户:$user2" #旧密码 oldPwd1=${oldPwd%|*} oldPwd2=${oldPwd#*} #新密码 newPwd1=${newPwd%|*} newPwd2=${newPwd#*} #更新第一个用户密码 mysql -u$user1 -p$oldPwd1 -e "alter user user() identified by '$newPwd1';" if [[ $? -eq 0 ]] then echo "$user1:PWD_UPDATE SUCCESS" else echo "$user1:PWD_UPDATE FAIL" exit 2 fi #更新第二个用户密码 mysql -u$user2 -p$oldPwd2 -e "alter user user() identified by '$newPwd2';" if [[ $? -eq 0 ]] then echo "$user2:PWD_UPDATE SUCCESS" else echo "$user2:PWD_UPDATE FAIL" exit 1 fi