Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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'
share|improve this answer
    
thanks u so much TLP :) :) Very nice explained –  user2091202 Feb 20 '13 at 13:17
    
@user2091202 You're welcome. –  TLP Feb 20 '13 at 13:18
    
But one thing that i would like to clear is how you have written bmake[9] and create_dirs.sh[2]. –  user2091202 Feb 20 '13 at 13:19
    
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 Feb 20 '13 at 13:23
    
@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". –  TLP Feb 20 '13 at 13:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.