user2091202
user2091202

Reputation: 33

Perl scripting in Linux bash scripting

I was reading one of the bash script where i have encountered the following lines. I was not able to guess what exactly these following lines are doing ? Can anyone give me some hint regarding what exactly these lines are doing. I have executed these lines separately but there is no output. I tried even using breakpoints.

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

I am looking forward for any kind response.

Thanks

Upvotes: 0

Views: 510

Answers (1)

TLP
TLP

Reputation: 67900

Its a script to keep track of identation. On the "Leaving" line, indent is decreased, on "Entering" they are increased. We then see that spaces are printed, based off the indent variable. In detail:

/usr/bin/perl -ne

-n flag puts a while(<>) loop around the script, which basically makes perl read from stdin or from argument files.

BEGIN { $|=1 }

Autoflush is turned on.

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

This regex here looks for lines such as

bmake[9] Leaving
create_dirs.sh[2] Leaving

When found, the $indent variable is decreased by 1.

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

This prints a space, repeated 4 * $indent times, followed by the input line.

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

This line increases indent by the same method as above.

More explanation on the regex (See it here, though I cleaned up the syntax from this site):

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'

Upvotes: 3

Related Questions