The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-28-2008
RockyC123 RockyC123 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 6
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.
  #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
  #3 (permalink)  
Old 08-28-2008
RockyC123 RockyC123 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 6
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.
  #4 (permalink)  
Old 08-28-2008
buffoonix buffoonix is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 145
Quote:
I forgot the `s when I copied it over from HP. It works with both `` and $(..)
Yes, because HP-UX's sh is a Posix shell. See man sh-posix to find out about its capabilities.
Therefore, you should rather use the $(...) style if you don't intend to run the script
unmodified on AIX's or SunOS/Solaris' sh.
  #5 (permalink)  
Old 08-28-2008
wempy's Avatar
wempy wempy is offline
Registered User
  
 

Join Date: Jun 2006
Location: Harpenden, UK
Posts: 208
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0