2013-02-20 154 views
0

我正在阅读其中一条bash脚本,其中遇到以下几行。我无法猜测这些以下几行内容在做什么?任何人都可以给我一些关于这些行正在做什么的暗示。我分别执行了这些行,但没有输出。我甚至尝试使用断点。在Linux bash脚本中的Perl脚本

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 }' 

我期待着任何回应。

谢谢

回答

3

它的脚本来跟踪身份。在“离开”行中,缩进缩小,在“进入”时增加。然后我们看到空格是基于缩进变量打印的。详细介绍:

/usr/bin/perl -ne 

-n标志把周围的脚本,基本上令Perl从标准输入或从参数文件中读取一个while(<>)循环。

BEGIN { $|=1 } 

Autoflush已打开。

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' 
+0

ü感谢这么多的TLP :) :)非常好的解释 – user2091202 2013-02-20 13:17:51

+0

@ user2091202不客气。 – TLP 2013-02-20 13:18:33

+0

但我想澄清的一件事是您如何编写bmake [9]和create_dirs.sh [2]。 – user2091202 2013-02-20 13:19:22