Shell脚本多行注释 [英] Shell script multi-line comment

查看:37
本文介绍了Shell脚本多行注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的 shell 脚本文件.有时在进行修改时,我想注释掉其中的一部分.但是下面示例中显示的注释行给了我错误.

I am having a large shell script file. At times while doing modification I want to comment out part of it. But commenting line as shown in the below example is giving me error.

脚本:

#!/bin/bash
<<COMMENT1
read build_label
read build_branch_tag
build_date_tag=$(echo $build_label | sed "s/$build_branch_tag//g")
echo $build_path
COMMENT1
echo "HELLO WORLD"

错误信息:

sed:第一个 RE 不能为空

sed: first RE may not be empty

我只是想了解上面的脚本有什么问题以及为什么评论部分不能正常工作.

I just want to understand what's wrong with the above script and why comment section is not working properly.

推荐答案

首先,用这里的文档来注释代码真的很脏!请改用 #.如果要注释多行,请使用编辑器.在 vim 中(例如注释 10 到 15 行):

First, using here docs to comment code is really dirty! Use the # instead. If you want to comment multiple lines, use your editor. In vim (commenting lines from 10 to 15 for example):

:10,15s/^/#

但是,要解决您当前的问题,您需要将开始的 here-doc 分隔符括在单引号中,如下所示:

However, to solve your current problem you need to enclose the starting here-doc delimiter in single quotes, like this:

<<'COMMENT'
...
COMMENT

使用单引号告诉 bash 它不应该尝试扩展 here 文档正文中的变量或表达式.

Using single quotes you tell bash that it should not attempt to expand variables or expression inside the here doc body.

这篇关于Shell脚本多行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆