0

我正在阅读我遇到以下行的 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 }'

我期待着任何善意的回应。

谢谢

4

1 回答 1

3

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

/usr/bin/perl -ne

-nflagwhile(<>)在脚本周围放置一个循环,这基本上使 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 }

该行通过与上述相同的方法增加缩进。

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

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'
于 2013-02-20T12:53:08.220 回答