I have written a very simple bash script to help me migrate from dev to staging. What it does is it deletes all files in staging, copies the files over from dev to stage.
我编写了一个非常简单的bash脚本来帮助我从dev迁移到staging。它的作用是删除分段中的所有文件,将文件从dev复制到阶段。
However, the config.inc.php file needs to have the first instance of "dev" to be changed to "stage", and no other instance changed.
但是,config.inc.php文件需要将“dev”的第一个实例更改为“stage”,并且不会更改其他实例。
Second, everytime I run it (I run the script from the dev directory), i'd like it to write a log back in the dev directory which will append the date/time stamp that I ran the staging bash script into this log.
其次,每次我运行它(我从dev目录运行脚本),我希望它在dev目录中写回一个日志,它会将我运行staging bash脚本的日期/时间戳附加到此日志中。
Thanks.
3 个解决方案
#1
4
This will change only the first appearance of "dev" to "stage"
这只会改变“dev”到“stage”的第一次出现
sed -i '0,/dev/ s/dev/stage/' config.inc.php
Be aware that it changes "devel" into "stageel". This version behaves just like you want, only a "dev" is searched, not a "devel" (in fact, s/\<dev\>/stage/
as the substitution expression should work, but it does not seem to work as expected? I'll be glad if anyone with more sed-fu can explain. )
请注意,它会将“devel”更改为“stageel”。这个版本的行为就像你想要的那样,只搜索“dev”,而不是“devel”(事实上,s / \
sed -i "/\<dev\>/,/\<dev\>/ s/dev/stage/" config.inc.php
For logging:
date >> /path/to/dev/run.log
Added by Jonathan Leffler
Jonathan Leffler补充道
- Assuming other issues are resolved (see below), the second
sed
command can still changedevel
tostagel
if the line contains, for example, "move devel code from /some/dev/location to /some/stage/location". - Also, the second
sed
command will map eachdev
found between the first line containingdev
and the second such line. This matters if there's more than one matching line, whereas the original '0,/dev/
' (or amended '0,/\<dev\>/
') only matches the first line as requested. - The reason
"s/\<dev\>/stage/"
doesn't work is not ased
issue but a shell issue. Use single quotes and you'd be almost OK. With double quotes, the back-slash less-than sequence appears tosed
as just less-than. -
Rule of Thumb: use single quotes around any argument in a shell script containing regular expression material. Unlesss it is saturated with single quotes, replace each single quote in the regular expression with the sequence quote, backslash, quote, quote "
'\''
". (The first quote terminates the single quote string; the backslash quote is a single quote; the last quote restarts the single quote string.) - Note that the '
-i
' option is a GNU extension tosed
; it is a legimate part of the answer since the question is tagged Linux, where GNUsed
is used; be aware if you need to move to a platform such as Solaris, AIX, HP-UX. - Finally,
sed
does not support extended regular expressions as standard; you have to explicitly enable them in GNU sed with the '-r
' option.
假设其他问题已得到解决(见下文),如果该行包含“将devel代码从/ some / dev / location移动到/ some / stage / location”,则第二个sed命令仍然可以将devel更改为stagel。
此外,第二个sed命令将映射在包含dev的第一行和第二行之间找到的每个dev。如果有多个匹配行,则这很重要,而原始的'0,/ dev /'(或修正的'0,/ \
“s / \
经验法则:在包含正则表达式材质的shell脚本中的任何参数周围使用单引号。除非它被单引号括起来,否则用正则表达式中的每个单引号替换序列引号,反斜杠,引号,引用“'\''”。 (第一个引号终止单引号字符串;反斜杠引号是单引号;最后一个引号重新启动单引号字符串。)
注意'-i'选项是sed的GNU扩展;它是答案的一个合法部分,因为这个问题标记为Linux,其中使用了GNU sed;请注意是否需要迁移到Solaris,AIX,HP-UX等平台。
最后,sed不支持扩展正则表达式作为标准;你必须使用'-r'选项在GNU sed中明确启用它们。
In my estimation, assuming overwrite is desirable, the command should be:
在我的估计中,假设需要覆盖,命令应该是:
sed -i -r '0,/\<dev\>/s/\<dev\>/stage/' config.inc.php
#2
1
To change a word in a file:
要更改文件中的单词:
cat config.inc.php | sed 's:dev::stage' > config.inc.php.new
To append to a log file:
要附加到日志文件:
echo $timestamp >> mylogfile.log
#3
0
The first part (editing your file) can be done well with the stream editor utility sed
:
使用流编辑器实用程序sed可以很好地完成第一部分(编辑文件):
sed -i -e s/dev/stage/ config.inc.php
This edits the file in-place (-i
) with no backup, using expression /dev/stage/
(-e
) to replace one expression with another.
这样就可以在没有备份的情况下就地编译文件(-i),使用表达式/ dev / stage /( - e)将一个表达式替换为另一个表达式。
#1
4
This will change only the first appearance of "dev" to "stage"
这只会改变“dev”到“stage”的第一次出现
sed -i '0,/dev/ s/dev/stage/' config.inc.php
Be aware that it changes "devel" into "stageel". This version behaves just like you want, only a "dev" is searched, not a "devel" (in fact, s/\<dev\>/stage/
as the substitution expression should work, but it does not seem to work as expected? I'll be glad if anyone with more sed-fu can explain. )
请注意,它会将“devel”更改为“stageel”。这个版本的行为就像你想要的那样,只搜索“dev”,而不是“devel”(事实上,s / \
sed -i "/\<dev\>/,/\<dev\>/ s/dev/stage/" config.inc.php
For logging:
date >> /path/to/dev/run.log
Added by Jonathan Leffler
Jonathan Leffler补充道
- Assuming other issues are resolved (see below), the second
sed
command can still changedevel
tostagel
if the line contains, for example, "move devel code from /some/dev/location to /some/stage/location". - Also, the second
sed
command will map eachdev
found between the first line containingdev
and the second such line. This matters if there's more than one matching line, whereas the original '0,/dev/
' (or amended '0,/\<dev\>/
') only matches the first line as requested. - The reason
"s/\<dev\>/stage/"
doesn't work is not ased
issue but a shell issue. Use single quotes and you'd be almost OK. With double quotes, the back-slash less-than sequence appears tosed
as just less-than. -
Rule of Thumb: use single quotes around any argument in a shell script containing regular expression material. Unlesss it is saturated with single quotes, replace each single quote in the regular expression with the sequence quote, backslash, quote, quote "
'\''
". (The first quote terminates the single quote string; the backslash quote is a single quote; the last quote restarts the single quote string.) - Note that the '
-i
' option is a GNU extension tosed
; it is a legimate part of the answer since the question is tagged Linux, where GNUsed
is used; be aware if you need to move to a platform such as Solaris, AIX, HP-UX. - Finally,
sed
does not support extended regular expressions as standard; you have to explicitly enable them in GNU sed with the '-r
' option.
假设其他问题已得到解决(见下文),如果该行包含“将devel代码从/ some / dev / location移动到/ some / stage / location”,则第二个sed命令仍然可以将devel更改为stagel。
此外,第二个sed命令将映射在包含dev的第一行和第二行之间找到的每个dev。如果有多个匹配行,则这很重要,而原始的'0,/ dev /'(或修正的'0,/ \
“s / \
经验法则:在包含正则表达式材质的shell脚本中的任何参数周围使用单引号。除非它被单引号括起来,否则用正则表达式中的每个单引号替换序列引号,反斜杠,引号,引用“'\''”。 (第一个引号终止单引号字符串;反斜杠引号是单引号;最后一个引号重新启动单引号字符串。)
注意'-i'选项是sed的GNU扩展;它是答案的一个合法部分,因为这个问题标记为Linux,其中使用了GNU sed;请注意是否需要迁移到Solaris,AIX,HP-UX等平台。
最后,sed不支持扩展正则表达式作为标准;你必须使用'-r'选项在GNU sed中明确启用它们。
In my estimation, assuming overwrite is desirable, the command should be:
在我的估计中,假设需要覆盖,命令应该是:
sed -i -r '0,/\<dev\>/s/\<dev\>/stage/' config.inc.php
#2
1
To change a word in a file:
要更改文件中的单词:
cat config.inc.php | sed 's:dev::stage' > config.inc.php.new
To append to a log file:
要附加到日志文件:
echo $timestamp >> mylogfile.log
#3
0
The first part (editing your file) can be done well with the stream editor utility sed
:
使用流编辑器实用程序sed可以很好地完成第一部分(编辑文件):
sed -i -e s/dev/stage/ config.inc.php
This edits the file in-place (-i
) with no backup, using expression /dev/stage/
(-e
) to replace one expression with another.
这样就可以在没有备份的情况下就地编译文件(-i),使用表达式/ dev / stage /( - e)将一个表达式替换为另一个表达式。