【发布时间】:2017-01-27 02:03:22
【问题描述】:
在我的源文件中,有多个 if 子句具有相同的 check 。我想通过注释掉基于 if 语句后定义的文本的条件语句来使条件块之一一直执行。
if [ "${SVR_GRP}" = "obi" ] ; then
EXTRA_JAVA_PROPERTIES="-Doracle.fusion.appsMode=true ${EXTRA_JAVA_PROPERTIES}"
export EXTRA_JAVA_PROPERTIES
fi
if [ "${SVR_GRP}" = "obi" ] ; then
EXTRA_JAVA_PROPERTIES="-DUseSunHttpHandler=true ${EXTRA_JAVA_PROPERTIES}"
export EXTRA_JAVA_PROPERTIES
fi
替换为
if [ "${SVR_GRP}" = "obi" ] ; then
EXTRA_JAVA_PROPERTIES="Doracle.fusion.appsMode=true ${EXTRA_JAVA_PROPERTIES}"
export EXTRA_JAVA_PROPERTIES
fi
#if [ "${SVR_GRP}" = "obi" ] ; then
EXTRA_JAVA_PROPERTIES="-DUseSunHttpHandler=true ${EXTRA_JAVA_PROPERTIES}"
export EXTRA_JAVA_PROPERTIES
#fi
您能否建议我如何使用 perl/python 或 shell 脚本来做到这一点?
我尝试了对我不起作用的 perl 命令,
perl -0pe '/if [ "${SVR_GRP}" = "obi" ] ; then\nEXTRA_JAVA_PROPERTIES="-DUseSunHttpHandler=true ${EXTRA_JAVA_PROPERTIES}"/#if [ "${SVR_GRP}" = "obi" ] ; then\nEXTRA_JAVA_PROPERTIES="-DUseSunHttpHandler=true ${EXTRA_JAVA_PROPERTIES}"
Python 程序按预期工作,但认为它可以解决,感觉可以有更好的解决方案。我不擅长shell脚本和perl。
file_loc = 'D:/official/workspace/pythontest/test/oops/test.sh'
new_file_loc = 'D:/official/workspace/pythontest/test/oops/test1.sh'
def modify():
file = open(file_loc)
file2 = open(new_file_loc, 'w')
lines = []
count = -1;
found = False
for line in file:
if str(line).strip() == 'if [ "${SVR_GRP}" = "obi" ] ; then':
count = 3;
lines.append(line)
else:
if str(line).strip() == 'EXTRA_JAVA_PROPERTIES="-DUseSunHttpHandler=true ${EXTRA_JAVA_PROPERTIES}"':
found = True
lines.append(line)
count = count - 1;
if (count == 0):
writeIntoFile(file2, lines, found)
found = False
count = -1
lines = []
elif count < 0:
lines = []
file2.write(line)
def writeIntoFile(file, lines, found):
for line in lines:
if found == False:
file.write(line)
elif str(line).strip() == 'if [ "${SVR_GRP}" = "obi" ] ; then' or str(line).strip() == 'fi':
file.write('#' + line);
else:
file.write(line)
modify()
【问题讨论】:
-
你尝试了什么?
-
请编辑您的问题并添加您尝试过的代码。在评论中阅读它几乎是不可能的。
-
@DaveCross 您可能希望为新用户添加
[edit]快捷方式,以帮助他们找到edit 的方法。 ;) -
我一直认为类似编辑的标签非常清楚:-) 但是,是的,我会在以后添加它。
-
@Rakeshnair:既然我们已经告诉过你不要将代码放在评论中,你为什么还要这样做?
标签: python perl shell python-2.4