#!/bin/bash DB_PASSWORD="" DB_PASSWORD_LEN="" SQL_RESULT="" SQL_RESULT_LEN="" #use a function to get or set the DB_PASSWORD # ......... DB_PASSWORD_LEN=${#DB_PASSWORD} #根据长度判断密码是否为空,采用不同的语句处理 if [[ ${DB_PASSWORD_LEN} -eq 0 ]];then SQL_RESULT=${mysql -u ${DB_USER} -e quit 2>&1} else SQL_RESULT=${mysql -u ${DB_USER} -p${DB_PASSWORD} -e quit 2>&1} fi #这里的-p和{DB_PASSWORD}必须挨着,不然会提示你输密码的 #密码正确的话,SQL_RESULT_LEN为0 SQL_RESULT_LEN=${#SQL_RESULT} if[[ ${SQL_RESULT_LEN} -eq 0 ]];then echo "Right Password !" else echo "Wrong Password !" fi