我想自动添加mariadb数据库条目,而使用expect脚本我有一些错误。 我期待的剧本:
#!/usr/bin/expect
spawn /usr/bin/mysql -u root -p
expect "Enter password:"
send "123456 \r"
expect "MariaDB [(none)]>"
send "create database zabbixdb character set utf8;\r"
expect "MariaDB [(none)]>"
send "grant all privileges on zabbixdb.* to 'zabbixuser'@'localhost' identified by '123456';\r"
expect "MariaDB [(none)]>"
send "flush privileges;\r"
expect "MariaDB [(none)]>"
send "exit\r"
但它一直失败,错误信息:
spawn /usr/bin/mysql -u root -p
Enter password: invalid command name "(none)"
while executing
"(none)"
invoked from within
"expect "MariaDB [(none)]>""
(file "./mariadb.sh" line 9)
我做错了什么?
答案 0 :(得分:1)
从错误消息中看,问题是因为未转义的空方括号[
。如果你想匹配文字的开放方括号,那么它应该是,
"MariaDB \\\[(none)]>"
原因是,[
对Tcl
和Expect
都很特殊。除非转义,否则它将被视为命令调用。
将您的代码更改为,
expect "MariaDB \\\[(none)]>"
注意:我们不需要逃离近方括号。
答案 1 :(得分:0)
解决了这个问题,我的剧本:
#!/usr/bin/expect
spawn mysql -u root -p123456
expect "MariaDB \\\[(none)]>"
send "create database zabbixdb character set utf8;\r"
expect "MariaDB \\\[(none)]>"
send "grant all privileges on zabbixdb.* to 'zabbixuser'@'localhost' identified by '123456';\r"
expect "MariaDB \\\[(none)]>"
send "flush privileges;\r"
expect "MariaDB \\\[(none)]>"
send "exit;\r"
主要问题是密码验证,请勿使用任何空格“-p123456”