【问题标题】:Perl scripting in Linux bash scriptingLinux bash 脚本中的 Perl 脚本
【发布时间】:2013-02-05 11:18:01
【问题描述】:

我正在阅读我遇到以下行的 bash 脚本之一。我无法猜测以下这些行到底在做什么?谁能给我一些关于这些行到底在做什么的提示。我已经分别执行了这些行,但没有输出。我什至尝试使用断点。

ssh $HOST bash -e <<
'END' 2>&1 |
 /usr/bin/perl -ne
 'BEGIN { $|=1 } ; 

if (/(bmake|create_dirs\.sh)\[\d+\] Leaving/)
 { --$indent };
 print " "x($indent * 4), "$_" ;
 if (/(bmake|create_dirs\.sh)\[\d+\] Entering/) { ++$indent }'

我期待任何善意的回应。

谢谢

【问题讨论】:

    标签: linux perl unix scripting


    【解决方案1】:

    它是一个记录身份的脚本。在“离开”行,缩进减少,在“进入”行,缩进增加。然后我们看到基于缩进变量打印了空格。详细:

    /usr/bin/perl -ne
    

    -n 标志在脚本周围放置了一个while(&lt;&gt;) 循环,这基本上使 perl 从标准输入或参数文件中读取。

    BEGIN { $|=1 }
    

    自动刷新已打开。

    if (/(bmake|create_dirs\.sh)\[\d+\] Leaving/) { --$indent };
    

    这个正则表达式在这里寻找诸如

    之类的行
    bmake[9] Leaving
    create_dirs.sh[2] Leaving
    

    找到时,$indent 变量减 1。

    print " "x($indent * 4), "$_" ;
    

    这会打印一个空格,重复 4 * $indent 次,然后是输入行。

    if (/(bmake|create_dirs\.sh)\[\d+\] Entering/) { ++$indent }
    

    这行增加缩进的方法和上面一样。

    更多关于正则表达式的解释(参见here,虽然我从这个站点清理了语法):

    NODE                     EXPLANATION
    --------------------------------------------------------------------------------
      (                        group and capture to $1:
    --------------------------------------------------------------------------------
        bmake                  literal string 'bmake'
    --------------------------------------------------------------------------------
       |                       OR
    --------------------------------------------------------------------------------
        create_dirs\.sh        literal string 'create_dirs.sh'
    --------------------------------------------------------------------------------
      )                        end of $1
    --------------------------------------------------------------------------------
      \[                       literal string '['
    --------------------------------------------------------------------------------
      \d+                      digits (0-9) (1 or more times (matching
                               the most amount possible))
    --------------------------------------------------------------------------------
      \] Leaving               literal string '] Leaving'
    

    【讨论】:

    • 但我想澄清的一件事是您是如何编写 bmake[9] 和 create_dirs.sh[2] 的。
    • 实际上在脚本中它是这样写的 (/(bmake|create_dirs\.sh)[\d+] Leaving/) 但是你说这个正则表达式是 bmake[9] create_dirs.sh[2 ] 怎么样???
    • @user2091202 好吧,它没有写成[\d+],在这种情况下,它的意思是“数字或加号”。它写成\[\d+\],括号用反斜杠转义,因此它们的元字符状态被暂停。它现在的意思是“一个文字左括号,后跟一个或多个数字,然后是一个文字右括号”。
    • 我会在正则表达式上添加更多解释。
    猜你喜欢
    • 2013-07-13
    • 2012-07-23
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 2013-07-19
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多