我有一个sql文件,我想通过ssh脚本更新。我试图使用sed替换那里的一些文本。我没有收到错误,但文字没有替换。这是我正在使用的代码。
#!/bin/sh
echo -n "enter theme: "
read theme
echo -n "enter username: "
read user
echo -n "enter domain/url: "
read url
cd /home/$theme/
sed -i 's/\/{$theme}/\/{$user}/g' ./$theme.sql
sed -i 's/{$theme}.domain.net/{$url}/g' ./$theme.sql
所以,我试图用我引用的sql文件中的新变量$ user和$ url替换当前文本($ theme变量)。
以下是筛选和更新内容的sql片段:
LOCK TABLES `ivg4_options` WRITE;
/*!40000 ALTER TABLE `ivg4_options` DISABLE KEYS */;
INSERT INTO `ivg4_options` VALUES (1,'siteurl','http://megashop.domain.net/','yes'),(2,'blogname','Woo Commerce','yes'),(3,'blogdescription','Just another WooCommerce site','yes'),(37,'home','http://megashop.domain.net','yes'),(38,'category_base','','yes'),(40,'advanced_edit','0','yes'),(41,'comment_max_links','2','yes'),(42,'gmt_offset','0','yes'),(43,'default_email_category','1','yes'),(52,'use_trackback','0','yes'),(53,'default_role','subscriber','yes'),(54,'db_version','33055','yes'),(55,'uploads_use_yearmonth_folders','1','yes'),(56,'upload_path','/home/megashop/public_html/wp-content/uploads','yes')
答案 0 :(得分:0)
首先,摆脱“{”和“}”。你不需要它们。
第二个问题是:''
需要一个参数(至少有一些版本......这里似乎就是这种情况),所以你必须提供至少一个空字符串sed
如果你想忽略它。从 -i extension
Edit files in-place, saving backups with the specified extension.
If a zero-length extension is given, no backup will be saved. It
is not recommended to give a zero-length extension when in-place
editing files, as you risk corruption or partial content in situ-
ations where disk space is exhausted, etc.
手册页:
#!/bin/sh echo -n "enter theme: " read theme echo -n "enter username: " read user echo -n "enter domain/url: " read url cd /home/$theme/ sed -i '' 's/\/$theme/\/$user/g' ./$theme.sql sed -i '' 's/$theme.domain.net/$url/g' ./$theme.sql
通常它会产生一个有点神秘的错误消息,说“sed:-i可能不会与stdin一起使用”。你可能错过了这个......
这是最终的剧本:
if/then