The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 08-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Your command means OHOME=echo and while that assignment is in place, run the command $OHOME which of course is not a valid command.

To capture the output of a command, put it in backticks (ASCII 96, not regular straight quotes):


Code:
O_BASE=`echo $OHOME | awk -F"/" '{for(i=2;i<=NF;i++) {if($i=="product"){exit;} a=a"/"$i; }} '`

$OHOME should properly be in double quotes, learn that now and you will get fewer surprises later (although in this case it doesn't really make a difference).

If your shell allows $(...) instead of backticks `...` that is perhaps more readable, although nominally less portable to legacy Bourne shell.

Your awk script doesn't appear to do anything with the a part, if you mean to capture it, you need to print it.

To strip off everything after the first occurrence of /product/ you can use the shell's variable substitution mechanism:


Code:
O_BASE=${OHOME%/product/*}

(Your awk script skips the first component /aaa, is that intentional? You can remove that with another variable substitution, O_BASE=${O_BASE#/aaa})

Last edited by era; 08-28-2008 at 03:56 PM.. Reason: Variable substitution remarks