views:

56

answers:

3

I need to understand the following line of code:

BIN_DIR=`grep BIN_DIR= $SMLCM | head -1`

where $SMLCH contains a path

This is what I understood so far:

  • grep will produce some string(s), possible paths. What does grep do with BIN_DIR=?

  • the pathes are passed to head and all files within the paths will be used to extract their first line

  • something is evaluated (what exactly?) and stored in BIN_DIR

Best regards,

Simon

+1  A: 
  • grep will match every line containing BIN_DIR= inside the contents of the file named by the var $SMLCM
  • head will take the first line
  • this line (the result of the commands inside the back ticks) is re-assigned to BIN_DIR
AlberT
+1  A: 

This will find all occureneces of the literal string "BIN_DIR=" in the file specified in the $SMLCM variable and only take the first line of that output and assign it to BIN_DIR.

Note that this may not work the way you want it to since whats gets assiggned to BIN_DIR is some string like "BIN_DIR=blah" that is it may have the same effect as the following:

BIN_DIR="BIN_DIR=blah"

ennuikiller
A: 

This will extract only the value of $BIN_DIR the first time it appears in the file pointed to by $SMLCM and assign it to the variable $BIN_DIR in the current script:

BIN_DIR=$(sed -n 's/^BIN_DIR=\(.*\)$/\1/p' $SMLCM | head -n 1)
  • sed -n : run the stream editor and don't print each line as it appears
  • s/ : substitute
  • ^BIN_DIR= : a line that begins ("^") with the text "BIN_DIR="
  • \(.*\)$ : and ends ("$") with a group ("\( \)") of zero or more ("*") of any characters (".")
  • / : replace that match with
  • \1 : the contents of the first (in this case, only) group
  • /p : finish the substitution and print the results
Dennis Williamson