1833111 Members
3207 Online
110051 Solutions
New Discussion

"echo" usage

 
SOLVED
Go to solution
Krishnan Viswanathan
Frequent Advisor

"echo" usage

I am using "echo" command in my script to find out the return code.

Eg : cp -p dir1 dir2 |wc -l
echo $?

I have many cases where I need to used a pipe.

The second half after the pipe almost always succeeds, so the error code is always "0".(even if the first half fails). How I can find out if both sides of the pipe have succeeded or not ?

Thank you

Krishnan
3 REPLIES 3
Steven Sim Kok Leong
Honored Contributor

Re: "echo" usage

Hi,

Why not this:

#!/sbin/sh
if cp -p dir1 dir2 > /tmp/listing
then
echo "Copy is successful"
wc -l /tmp/listing
rm -f /tmp/listing
else
echo "Copy is unsuccessful"
fi

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: "echo" usage

Hi,

If you insist on using a pipe, then:

#!/sbin/sh
if cp -p dir1 dir2 && cp -p dir1 dir2 | wc -l
then
echo "Both copy and wordcount are successful."
else
echo "Either copy (most likely) or wordcount is unsuccessful."
fi

Hope this helps. Regards.

Steven Sim Kok Leong
Helen French
Honored Contributor
Solution

Re: "echo" usage

Hi Iyer,

Check this out:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x569d8b55dfd6d4118fef0090279cd0f9,00.html

HTH,
Shiju

Life is a promise, fulfill it!