Bash脚本,替换文件中的文本?

时间:2012-07-06 16:13:37

标签: bash ssh

我目前有

echo -n "MySQL Database Name:  "; read MyVar; sed -i 's/database_name_here/$MyVar/g' config.php

它部分有效,它会提示我输入数据库名称。一旦输入它就会更新文件,但它仍然会放置$ MyVar而不是输入的内容。

echo -n "MySQL Database Name:  "; read MyVar; echo "$Myvar"

^工作正常。

有什么想法吗?

更新:

echo -n MySQL Database Name: ; read MyVar; sed -i s/database_name_here/$MyVar/g config.php
# ////
echo -n MySQL Username: ; read usr; sed -i s/username_here/$usr/g config.php
# ////
echo -n MySQL Password: ; read blackberg; sed -i s/password_here/$blackberg/g config.php

如果它只是bash上的一行,它可以工作并替换该值。 但是当我有上述内容时,它会部分工作,但会将值替换为空白。

任何提示?

1 个答案:

答案 0 :(得分:4)

sed -i "s/database_name_here/$MyVar/g" config.php

要求shell解释变量,你需要使用双引号。