![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Bash Shell to sh Shell In a Script | saurabh84g | Shell Programming and Scripting | 8 | 08-29-2008 06:51 AM |
| different shell csh bash | ajp7701 | Shell Programming and Scripting | 1 | 04-18-2008 05:19 PM |
| c shell instead of bash | ajp7701 | Shell Programming and Scripting | 0 | 03-18-2008 04:15 PM |
| Bash Shell | BG_JrAdmin | Linux | 1 | 08-07-2006 02:56 PM |
| Bash shell ... | VijayHegde | Shell Programming and Scripting | 2 | 06-06-2006 09:09 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
bash shell piping
Hello all,
I am new to bash. I am trying to get a sub string of a variable in a shell script. While trying to do that I get the following error: ------------------------------------------------------------------------ OHOME: /aaa/bbb/product/eee ./t.sh: line 6: /aaa/bbb/product/eee: No such file or directory Oracle Base: ------------------------------------------------------------------------ Here is the code: ------------------------------------------------------------------------ #!/bin/sh OHOME=/aaa/bbb/product/eee echo " OHOME: "$OHOME O_BASE=echo $OHOME | awk -F"/" '{for(i=2;i<=NF;i++) {if($i=="product"){exit;} a=a"/"$i; }} ' echo " Oracle Base: "$O_BASE ------------------------------------------------------------------------ I am very surprised. Since it does not seem to like "echo $OHOME" I tried several things like enclosing in brackets, single/double quotes, etc. I still get this error. I cannot seem to find the syntax from the book or google ![]() Looks very simple, but I am stuck on this for hours. Maybe experts here can guide me. Here is our Linux version: Linux <name> 2.6.9-67.0.20.ELsmp #1 SMP Wed Jun 18 12:40:47 EDT 2008 i686 i686 i386 GNU/Linux Rocky. |
|
||||
|
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; }} '`
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/*}
Last edited by era; 08-28-2008 at 03:56 PM.. Reason: Variable substitution remarks |
|
||||
|
Ahhh.. Thanks a lot. I forgot the `s when I copied it over from HP. It works with both `` and $(..)
Your second suggestion "O_BASE=${OHOME%/product/*}" works perfectly without all the extras. I cannot understand that one - need to get a good book to learn that syntax. On the third part, the way this awk works is, it assumes the part before the first slash as a field: "/aaa/bbb/product/eee". In this one, aaa is the second field per awk and bbb is the third field. Problem resolved. THanks a lot for your help. |
|
||||
|
Quote:
Therefore, you should rather use the $(...) style if you don't intend to run the script unmodified on AIX's or SunOS/Solaris' sh. |
|
|||||
|
this Reference Cards apendix from the Advanced Bash Scripting Guide (http://tldp.org/LDP/abs/html/) has been a boon to me 'cos I can never remember the correct syntax for string slicing.
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|