a question about 'read' from pipe in shell script

a question about 'read' from pipe in shell script

Post by Mike Din » Thu, 25 Jul 2002 23:42:18



Hi, all,

i am stuck with 'read' in my shell script, and could figure out why it did
not work, here is my simple script:

------------------------------------------
#!/bin/sh

ORACLE_HOME=/apps/oracle/product/8.1.7

if [ `uname` -eq SunOS ]; then
echo "Inside"
   /usr/ccs/bin/size $ORACLE_HOME/bin/oracle | /usr/bin/awk '{print $1}'
|read mtext
fi

echo $mtext
-------------------------------------------
when i run this script, i get:
dev:sql%sh mem_size.sh
Inside

From command line, when i use 'size', the output is:

eds-xml-dev:sql%size $ORACLE_HOME/bin/oracle
27221933 + 267676 + 21060 = 27510669
eds-xml-dev:sql%size $ORACLE_HOME/bin/oracle | awk '{print $1}'
27221933

So it seems 'read' doesn't want to read anything from pipe, could somebody
shed light on this? thanks.

Mike

 
 
 

a question about 'read' from pipe in shell script

Post by Villy Kru » Thu, 25 Jul 2002 23:48:20


On Wed, 24 Jul 2002 10:42:18 -0400,

Quote:>Hi, all,

>i am stuck with 'read' in my shell script, and could figure out why it did
>not work, here is my simple script:

>------------------------------------------
>#!/bin/sh

>ORACLE_HOME=/apps/oracle/product/8.1.7

>if [ `uname` -eq SunOS ]; then
>echo "Inside"
>   /usr/ccs/bin/size $ORACLE_HOME/bin/oracle | /usr/bin/awk '{print $1}'
>|read mtext
>fi

>echo $mtext

You may need (real) ksh for this to work.  Other shells tend to make
variable assignment inside a conditional or loop block invisible outside
the block.

Appart from that: -eq is for numeric comparison.  Use = for string comparisons.

Villy

 
 
 

a question about 'read' from pipe in shell script

Post by Mike Din » Fri, 26 Jul 2002 00:35:24


Thanks, ksh does the job. Thanks a lot.


> On Wed, 24 Jul 2002 10:42:18 -0400,

> >Hi, all,

> >i am stuck with 'read' in my shell script, and could figure out why it
did
> >not work, here is my simple script:

> >------------------------------------------
> >#!/bin/sh

> >ORACLE_HOME=/apps/oracle/product/8.1.7

> >if [ `uname` -eq SunOS ]; then
> >echo "Inside"
> >   /usr/ccs/bin/size $ORACLE_HOME/bin/oracle | /usr/bin/awk '{print $1}'
> >|read mtext
> >fi

> >echo $mtext

> You may need (real) ksh for this to work.  Other shells tend to make
> variable assignment inside a conditional or loop block invisible outside
> the block.

> Appart from that: -eq is for numeric comparison.  Use = for string
comparisons.

> Villy

 
 
 

a question about 'read' from pipe in shell script

Post by Chris F.A. Johnso » Fri, 26 Jul 2002 02:38:46



> On Wed, 24 Jul 2002 10:42:18 -0400,

>>Hi, all,

>>i am stuck with 'read' in my shell script, and could figure out why it did
>>not work, here is my simple script:

>>------------------------------------------
>>#!/bin/sh

>>ORACLE_HOME=/apps/oracle/product/8.1.7

>>if [ `uname` -eq SunOS ]; then
>>echo "Inside"
>>   /usr/ccs/bin/size $ORACLE_HOME/bin/oracle | /usr/bin/awk '{print $1}'
>>|read mtext
>>fi

>>echo $mtext

> You may need (real) ksh for this to work.  Other shells tend to make
> variable assignment inside a conditional or loop block invisible outside
> the block.

    The reason it didn't work is because the elements of a pipe are,
    in most shells, executed in a subshell, and a subshell cannot
    affect its parent.

    One way around it is to enclose the read and echo commands in braces:

            | { read mtext; echo $mtext }

    You will find many posts about this, and many alternative methods,
    in the archives of this group, and in the FAQ.

Quote:> Appart from that: -eq is for numeric comparison.  Use = for string comparisons.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
 
 
 

a question about 'read' from pipe in shell script

Post by Philipp Buehle » Fri, 26 Jul 2002 07:56:51



>    /usr/ccs/bin/size $ORACLE_HOME/bin/oracle | /usr/bin/awk '{print $1}'
>|read mtext
[..]
> So it seems 'read' doesn't want to read anything from pipe, could somebody
> shed light on this? thanks.

Most probably subshell effect.
you can use
mtext=`/usr/ccs/bin/size $ORACLE_HOME/bin/oracle | /usr/bin/awk '{print $1}'`

anyway, if this still doesnt work, export ORACLE_HOME

ciao
--
"Unix was the first OS where you could carry the media and system
documentation around in a briefcase. This was fixed in BSD4.2."

 
 
 

1. unix shell script ignores 'exit' when in function that is piped to tee

Script ignores 'exit' when called from within a function that is
piped to tee

$ cat a.sh
#!/bin/sh
FUN1() {
uname -a
exit 0
echo "This and that..."
FUN1 | tee -a wco.txt
echo "SHOULD NOT SEE, BECAUSE FUN1 HAS EXIT IN IT."

$ a.sh
This and that...
HP-UX hqignau3 B.11.11 U 9000/800 1548756587 unlimited-user license
SHOULD NOT SEE, BECAUSE FUN1 HAS EXIT IN IT.

to the screen because function FUN1 calls the exit command and FUN1 is
called before the echo line for "SHOULD NOT SEE...".

I have scoured the Internet for an answer but came up empty handed.
I found these two pages...

http://www.blacksheepnetworks.com/security/resources/iaoq.html
B.4

http://home.comcast.net/~j.p.h/cus-faq.html#M

But I was unable to take the above and apply it successfully to my
problem of exit being ignored when it is piped thru tee.

POSIX.2 compliant.


will post a summary result if a fix is found that works for me.

2. Clipboard Interface under X11

3. 'read' timeouts in shell scripts

4. CELLular Service

5. Newbie: problem with 'read' in shell script

6. PPC gimp install

7. Convert 'a' to 'A' in a Shell-Script

8. Apache, suEXEC and mod_rewrite

9. question on 'read' function with Bourne Shell

10. Q: 2 questions of shell script('whichdir', $0)

11. K shell question...executing a script without 'period-slash'

12. What's 'side effects' of Ksh built-ins?

13. CERN Error 500: can't read script output pipe