0

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

1 Answer 1

3

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'
Sign up to request clarification or add additional context in comments.

4 Comments

But one thing that i would like to clear is how you have written bmake[9] and create_dirs.sh[2].
Actually in the script it's written like this (/(bmake|create_dirs\.sh)[\d+] Leaving/) but you saiid this regex is such as bmake[9] create_dirs.sh[2] HOW ???
@user2091202 Well, its not written [\d+], in which case it had meant "a number or a plus sign". Its written \[\d+\], with the brackets escaped with backslash, so that their meta character status is suspended. It now means "a literal opening bracket, followed by one or more numbers, followed by a literal closing bracket".
I'll add more explanation on the regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.