echo 全局替换的解决方案
如何将所有 ksh 脚本中的 echo
替换为 echo -e
。 示例:
输入
脚本包含以下几行,
echo "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
输出:
我不应干扰 set echo off 命令,而仅更改打印 echo 命令。如果已经更改为echo -e
,我不应该再次更换。我应该一次性替换根目录中的所有脚本。
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
How do I replace echo
in all my ksh scripts to echo -e
.
Example:
Input
Script containing following lines,
echo "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
OUTPUT:
I should not disturb set echo off command and only change the printing echo commands. If already it is changed to echo -e
, I should not replace again. I should replace it in a single shot for all the scripts from root directory.
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该有效 -
测试:
find
和exec
:This should work -
Test:
find
andexec
:查找
echo
后跟一个空格和破折号之外的其他内容;将echo
替换为echo -e
。将其循环应用到您要更改的文件。Look for
echo
followed by a space and something other than a dash; replace theecho
withecho -e
. Apply it in a loop to the files you want to change.更强大的正则表达式将在任何上下文中查找
echo
后面不跟有-e
或off
:由于 sed 无法在正则表达式中进行环视,因此您必须使用 perl:
这也可以处理您可能遇到的情况(例如)echo -n - e 其中是一个
-e
,但不是直接在echo
之后:你不想以<代码>echo -e -n -e。另外,如果您执行了(例如)echo --version
,您就不会需要echo -e --version
。上面的正则表达式可以处理这些问题。输入
输出
A more robust regex would be look for
echo
not followed by-e
oroff
in any context:Since
sed
can't do lookarounds in regex you'd have to useperl
:This also handles the case that you might have (e.g.)
echo -n -e
where there is a-e
, but not directly after theecho
: you don't want to end up withecho -e -n -e
. Also, if you did (say)echo --version
, you wouldn't want aecho -e --version
. The above regex takes care of these.INPUT
OUTPUT