加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 正则表达式从Bash脚本中删除注释

发布时间:2020-12-14 06:24:05 所属栏目:百科 来源:网络整理
导读:这看起来很复杂.我需要一个正则表达式来从 Bash shell脚本中删除注释. 请记住$#,${#foo},string =“this#string”,string =’that#string’,${foo#bar},${foo ## baar},以及 string="really complex args=$# ${applejack##"jack"} $(echo "$#,again")"; `ech
这看起来很复杂.我需要一个正则表达式来从 Bash shell脚本中删除注释.

请记住$#,${#foo},string =“this#string”,string =’that#string’,${foo#bar},${foo ## baar},以及

string="really complex args=$# ${applejack##"jack"} $(echo "$#,again")"; `echo this is a ${#nasty[*]} example`

都是有效的shell表达式,不应该被剥离.

编辑:
注意:

# This is a comment in bash
  # But so is this
echo "foo bar" # This is also a comment

编辑:
请注意,可能被误解为评论的行可能隐藏在HEREDOC内部,但由于它是多行的,我可以在不处理/解释它的情况下生活:

cat<<EOF>>out.txt
This is just a heredoc
# This line looks like a comment,but it isn't
EOF
你不能用正则表达式做到这一点.
echo ${baz/${foo/${foo/#bar/foo}/bar}/qux}

您需要匹配嵌套大括号.正则表达式不能这样做,除非你愿意考虑PCRE的“正则表达式”,在这种情况下,在Perl中编写解析器会更简单.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读