我有一个 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 命令都会在下面的脚本中执行。那么,为什么上面的脚本会出现Error 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
答案1
该问题与数组无关;这是引用。
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';
如果你可以使用is 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};"'