shell脚本——注释(单行注释 多行注释)

2023-05-16

单行注释

以"#"开头的行就是注释,会被解释器忽略。


#--------------------------------------------
# 这是一个注释
# author:菜鸟教程
# site:www.runoob.com
# slogan:学的不仅是技术,更是梦想!
#--------------------------------------------
##### 用户配置区 开始 #####
#
#
# 这里可以添加脚本描述信息
# 
#
##### 用户配置区 结束  #####  

多行注释

方法一:

:<<!

#被注释的内容   

!


方法二:

:'

#被注释的内容

'


方法三:

if false;then

#被注释的内容

fi


方法四:

:<<任意字符或者数字

#被煮熟的内容

任意字符或者数字


方法五:

((0)) && {

#被注释的内容

}

    [root@J01051386 shell]# cat -n zhushi.sh  

    1 #!/bin/sh

     2 trap 'echo "before execute line:$LINENO,"$1"=$1"' DEBUG 
     3 #方法一
     4 echo aa
     5 :<<!
     6 echo bb
     7 echo cc
     8 !
     9 echo dd
    10
    11 #方法二
    12 echo a
    13 :'
    14 echo b
    15 echo c
    16 '
    17 echo d

    18
    19 #方法三
    20 echo aaa
    21 if false;then
    22 echo bbb
    23 echo ccc
    24 fi
    25 echo ddd
    26
    27 #方法四 :<<任意字符或者数字
    28 echo aaaa
    29 :<<#
    30 echo bbbb
    31 echo cccc
    32 #
    33 echo dddd
    34
    35 #方法五
    36 echo aaaaa
    37 ((0)) && {
    38 echo bbbbb
    39 echo ccccc
    40 }
    41 echo ddddd
[root@J01051386 shell]# sh -x  zhushi.sh 
+{2:} trap 'echo "before execute line:$LINENO,"$1"=$1"' DEBUG
++{4:} echo 'before execute line:4,='
before execute line:4,=
+{4:} echo aa
aa
++{5:} echo 'before execute line:5,='
before execute line:5,=
+{5:} :
++{9:} echo 'before execute line:9,='
before execute line:9,=
+{9:} echo dd
dd
++{12:} echo 'before execute line:12,='
before execute line:12,=
+{12:} echo a
a
++{16:} echo 'before execute line:16,='
before execute line:16,=
+{16:} ':
echo b
echo c
'
zhushi.sh: line 16: :
echo b
echo c
: command not found

++{17:} echo 'before execute line:17,='
before execute line:17,=
+{17:} echo d
d
++{20:} echo 'before execute line:20,='
before execute line:20,=
+{20:} echo aaa
aaa
++{21:} echo 'before execute line:21,='
before execute line:21,=
+{21:} false
++{25:} echo 'before execute line:25,='
before execute line:25,=
+{25:} echo ddd
ddd
++{28:} echo 'before execute line:28,='
before execute line:28,=
+{28:} echo aaaa
aaaa

综上所述:需要在自己编译环境下,亲测每种注释方法的有效性。

据参考文件中所述,在实际linux环境中测试结果,可能linux版本的区别,并不会所有场景都兼容...、、、、、

trap 'command' DEBUG的调试方式,参考文章shell脚本——调试(-n / -x /-c)

参考链接:https://blog.csdn.net/lansesl2008/article/details/20558369/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

shell脚本——注释(单行注释 多行注释) 的相关文章

随机推荐