bash shell piping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash shell piping
# 1  
Old 08-28-2008
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 Smilie

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  
Old 08-28-2008
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  
Old 08-28-2008
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  
Old 08-28-2008
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  
Old 08-28-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

3. Programming

Linux Shell Piping w/Shared Memory

So I am pretty new to the linux environment, and I am trying to create a shell that uses multiple pipes, and I read online that piping using shared memory space is more efficient than using regular piping. However, I have zero clue how to use shared memory space with pipes. Has anyone done this... (1 Reply)
Discussion started by: Greg_MC
1 Replies

4. UNIX for Dummies Questions & Answers

Which of the following command displays your login shell in bash shell?

Options:: A)$shell B)echo $ bash C)echo $ O D)$ O (1 Reply)
Discussion started by: raghugowda
1 Replies

5. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

6. Shell Programming and Scripting

Exit code from piping in unix shell script

Hi , I have following code in my shell script : "$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p1.stx" "${TS_LOGS}/tranfrmr_p1.err" | ( "$TS_BIN/cusparse" "${TS_SETTINGS}/cusparse_p2.stx" "${TS_LOGS}/cusparse_p2.err" | ( "$TS_BIN/tsqsort" "${TS_SETTINGS}/srtforpm_p3.stx"... (8 Replies)
Discussion started by: sonu_pal
8 Replies

7. Shell Programming and Scripting

Piping output from a command into bash script

Hi all. I am using procmail to deliver an email to a script I am developing. Procmail delivers the email to the script on standard input. I imagine this is the same as piping input from a command into the script. Hence I've been testing my script by running echo 'test' | sms-autosend-backup.sh ... (2 Replies)
Discussion started by: akindo
2 Replies

8. Shell Programming and Scripting

I need to understand the differences between the bash shell and the Bourne shell

I do not claim to be an expert, but I have done things with scripts that whole teams of folks have said can not be done. Of course they should have said we do not have the intestinal fortitude to git-r-done. I have been using UNIX actually HPUX since 1992. Unfortunately my old computer died and... (7 Replies)
Discussion started by: awk_sed_hello
7 Replies

9. Shell Programming and Scripting

Piping from BASH in to an Instant Messenger??

Is it possible to pipe a command in to an instant messenger e.g pidgin, finch or something similar and have it send??? e.g echo "hello" | messenger Or is there anything similar?? Thanks in advance! (1 Reply)
Discussion started by: 64mb
1 Replies

10. Shell Programming and Scripting

Run a C Shell using only Bash shell

Hi, I have only bash on my system. And I want to run a C shell... Is there a way for that? (3 Replies)
Discussion started by: HSN
3 Replies
Login or Register to Ask a Question