前提・実現したいこと
コマンドラインからデータベースを操作する(特にシェルスクリプトを使って)際に、
「test-user」などの記号を含むユーザを作成できるようにする。
発生している問題・エラーメッセージ
問題となっているのは下記の部分のシェルスクリプト。
#!/bin/bash DatabaseHost=localhost DatabaseUser="test-user" mysql -uroot -e "create user $DatabaseUser@$DatabaseHost identified by 'wordpress';"
[root@localhost ~]# sql.sh ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '-user@test identified by 'wordpress'' at line 1
変数「DatabaseUser」が「test」など記号を含まないものであれば
test@localhostと記述できるため正常に作成できる。
しかし、記号を含むユーザ名だと
"test-user"@"localhost"や'test-user'@'localhost'と
クォーテーションを付ける必要があり、
変数が正常に展開されず、もしくはコマンドラインからの操作につけた「-e ""」を競合してエラーとなる。
試したこと
mysql -uroot -e "create user \'$DatabaseUser\'@\'$DatabaseHost|' identified by 'wordpress';"
エスケープの仕方が間違っているのか、解決せず。
パスワードもできれば変数に入れたいが、シングルクォーテーションのため変数展開できない。
補足情報
・MariaDBのバージョン
[root@localhost ~]# rpm -qa mariadb
mariadb-5.5.60-1.el7_5.x86_64
・Centosのバージョン
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/24 06:06