我正在制作一个bash脚本,需要一些帮助,这就是我现在所拥有的。
mysql -e "create database if not exists ${UN}_${SQLBASE}"
mysql -Be "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, CREATE ROUTINE ON \`"$UN"\\_"$SQLBASE"\`.* TO '"$UN"_"$SQLUSER"'@'localhost' IDENTIFIED BY '$SQLPASS';FLUSH PRIVILEGES"
差不多,我需要知道如何创建一个名为cpanel_db1的数据库,如果存在,则自动向上移动到数字。 ex cpanel_db2。
答案 0 :(得分:0)
可以这样做:
username="usernamedb"
password="passworddb"
database="cpanel_db"
databases=$(mysql -u $username -p$password -Bse 'show databases'|grep $database)
max=1
if [ -z "$databases" ];then
mysql -u $username -p$password -Bse "create database ${database}$max"
else
for cdatabase in $databases
do
cdatabasenumber=$(echo $cdatabase|awk -F"db" '{print $2}')
if [ $cdatabasenumber -gt $max ]; then
max=$cdatabasenumber
fi
done
let max++
mysql -u $username -p$password -Bse "create database ${database}$max"
fi
〜 〜