BASH echo写入mysql输入

时间:2010-12-30 01:22:02

标签: bash

有一个bash菜单,其中变量写入文件以进行mysql输入。 继承人我所拥有的:

    echo "CREATE DATABASE '$mysqldbn';

#GRANT ALL PRIVILEGES ON *.* TO '$mysqlu'@'$myhost' IDENTIFIED BY '$mysqlup' WITH GRANT OPTION;

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON '$mysqldbn'.* TO '$mysqlu'@'$myhost' IDENTIFIED BY '$mysqlup';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON '$mysqldbn'.* TO '$mysqlu'@'$myip' IDENTIFIED BY '$mysqlup';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON '$mysqldbn'.* TO '$mysqlu'@'localhost' IDENTIFIED BY '$mysqlup';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES< LOCK TABLES on '$mysqldbn'.* TO '$mysqlu'@'$rip' IDENTIFIED BY '$mysqlup';" > nmysql.db


mysql -u root -p$mypass < nmysql.db

问题是获取变量以显示我必须将它们放在单引号中,单引号显示为我想要的'$ mysqlu'@'localhost'等实例。但是如何删除引号仍然可以在实例中使用变量,例如CREATE DATABASE'$ mysqldbn'?双引号也不会起作用,我不知所措。

提前致谢, 乔

3 个答案:

答案 0 :(得分:1)

我在这里看不到您的问题,但您可以使用${mysqldbn}在bash中引用变量名称,这会有帮助吗?

答案 1 :(得分:0)

您应该可以省略不需要它们的单引号。

但我建议使用这里的文件。大约:

mysql -u root -p$mypass <<EOF

CREATE DATABASE $mysqldbn;

#GRANT ALL PRIVILEGES ON *.* TO '$mysqlu'@'$myhost' IDENTIFIED BY '$mysqlup' WITH GRANT OPTION;

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON '$mysqldbn'.* TO '$mysqlu'@'$myhost' IDENTIFIED BY '$mysqlup';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON '$mysqldbn'.* TO '$mysqlu'@'$myip' IDENTIFIED BY '$mysqlup';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON '$mysqldbn'.* TO '$mysqlu'@'localhost' IDENTIFIED BY '$mysqlup';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES< LOCK TABLES on '$mysqldbn'.* TO '$mysqlu'@'$rip' IDENTIFIED BY '$mysqlup';
EOF

答案 2 :(得分:0)

据我所知,你必须使用双引号来显示值,这是我的例子:

#!/bin/bash
myvar="MyValue"
echo "$myvar"
echo '$myvar'
echo '${myvar}'

生成:

MyValue
$myvar
${myvar}

希望这有帮助