Bash

Bash非互動式mysql數據庫創建問題

  • November 17, 2021

我有一個 bash 腳本,它利用對話框在非互動式 Mysql 數據庫創建期間顯示進度條。

系統作業系統:Ubuntu 20.04 伺服器

Mariadb:10.5

問題:

只有第一個 Mysql 命令被執行(即創建數據庫),在其他情況下,有Error 1064.

#!/usr/bin/env bash
export NCURSES_NO_UTF8_ACS=1
var_db() {
   pfx=$(shuf -zer -n2 {a..z} | tr -d '\0')
   vnum=$(shuf -i 8-11 -n 1)
   sfx=$(openssl rand -base64 32 | tr -d /=+ | cut -c -"$vnum")
   varnam="${pfx}_${sfx}"
   echo "$varnam"
}
site_db() {
   dbname=$(var_db)
   userdb=$(var_db)
   vnum=$(shuf -i 12-15 -n 1)
   passdb=$(openssl rand -base64 32 | tr -d /=+ | cut -c -"$vnum")
   msgs=('Database created'
   'Database user created'
   'All privileges granted on database to user'
   'Reload privilege tables'
   'Restart Mariadb'
   'Reload Mariadb'
       )
   commands=('sudo mysql -e "CREATE DATABASE ${dbname};"'
   'sudo mysql -e "CREATE USER ${userdb}@localhost IDENTIFIED BY '${passdb}';"'
   'sudo mysql -e "GRANT ALL PRIVILEGES ON ${dbname}.* TO '${userdb}'@'localhost';"'
   'sudo mysql -e "FLUSH PRIVILEGES;"'
   'sudo systemctl restart mariadb'
   'sudo systemctl reload mariadb'
           )
   n=${#commands[@]}
   i=0
   while [ "$i" -le "$n" ]; do
       pct=$(( i * 100 / n )) # calculating progress percentage
       if [ $pct -eq 100 ]; then
           echo -e "XXX\n100\nMariadb secure installation completed!\nXXX"
       else
           echo -e "XXX\n$i\n${msgs[i]}\nXXX"
       fi
       echo "$pct"
       eval "${commands[i]}" > /dev/null 2>&1 #eval the output of the commands, 
       i=$((i + 1))
       sleep 2
   done | dialog --keep-tite --title "Site database configuration" --gauge "Site database created" 20 100 0
}
site_db

但是,如果我不使用數組中的命令,那麼所有 mysql 命令都會在下面的腳本中執行。那麼,為什麼上面的腳本中會出現錯誤 1064?

#!/usr/bin/env bash
var_db() {
   pfx=$(shuf -zer -n2 {a..z} | tr -d '\0')
   vnum=$(shuf -i 8-11 -n 1)
   sfx=$(openssl rand -base64 32 | tr -d /=+ | cut -c -"$vnum")
   varnam="${pfx}_${sfx}"
   echo "$varnam"
}
site_db() {
   dbname=$(var_db)
   userdb=$(var_db)
   vnum=$(shuf -i 12-15 -n 1)
   passdb=$(openssl rand -base64 32 | tr -d /=+ | cut -c -"$vnum")
   sudo mysql -e "CREATE DATABASE ${dbname};"
   sudo mysql -e "CREATE USER ${userdb}@localhost IDENTIFIED BY '${passdb}';"
   sudo mysql -e "GRANT ALL PRIVILEGES ON ${dbname}.* TO '${userdb}'@'localhost';"
   sudo mysql -e "FLUSH PRIVILEGES;"
   sudo systemctl restart mariadb
           )
}
site_db

問題與數組無關;這是報價。

userdb=userdb_value
passdb=passdb_value
set -x

eval echo 'sudo mysql -e "CREATE USER ${userdb}@localhost IDENTIFIED BY '${passdb}';"'
+ eval echo 'sudo mysql -e "CREATE USER ${userdb}@localhost IDENTIFIED BY passdb_value;"'
++ echo sudo mysql -e 'CREATE USER userdb_value@localhost IDENTIFIED BY passdb_value;'
sudo mysql -e CREATE USER userdb_value@localhost IDENTIFIED BY passdb_value;

echo sudo mysql -e "CREATE USER ${userdb}@localhost IDENTIFIED BY '${passdb}';"
+ echo sudo mysql -e 'CREATE USER userdb_value@localhost IDENTIFIED BY '\''passdb_value'\'';'
sudo mysql -e CREATE USER userdb_value@localhost IDENTIFIED BY 'passdb_value';

如果您可以使用"'不是 SQL,那麼您可以這樣做:

'sudo mysql -e "CREATE USER ${userdb}@localhost IDENTIFIED BY \"${passdb}\";"'

如果沒有,那麼你可以這樣做

'sudo mysql -e "CREATE USER ${userdb}@localhost IDENTIFIED BY '\''${passdb}'\'';"'

或者

"sudo mysql -e \"CREATE USER ${userdb}@localhost IDENTIFIED BY '${passdb}';\""

或者

quote=\'
'sudo mysql -e "CREATE USER ${userdb}@localhost IDENTIFIED BY ${quote}${passdb}${quote};"'

引用自:https://unix.stackexchange.com/questions/678011