Operating System - HP-UX
1752795 Members
6034 Online
108789 Solutions
New Discussion юеВ

output redirection and wc

 
SOLVED
Go to solution
sekar sundaram
Honored Contributor

output redirection and wc

Hi,

a simple question...
for i in NODE `cat nodeslist`
do
echo `date` >>HPDev.policies
eecho Node Name - $NODE >> HPDev.policies
ovdeploy -cmd "opctemplate -list" -node $NODE >>HPDev.policies 2>>HPDev.Policies.error | wc >>HPDev.policies
done

i am running a command to get list of policies on a node and sending it to a file. also the count of policies i would like to update.
but this "wc" does not work.
this above "wc" updates only 0 0 0



3 REPLIES 3
Steven Schweda
Honored Contributor
Solution

Re: output redirection and wc

> [...] but this "wc" does not work. [...]

It counts what it sees, but what does it see?

> [...] >>HPDev.policies
> [...] 2>>HPDev.Policies.error
> [...] | wc [...]

You seem to be sending stdout to one file,
and stderr to another file. Then you pipe
what's left into "wc", but what's left?
Everything has already been redirected to
some file or other.

Consider this simpler (all-shell) example:

dyi # ( echo stdout ; echo stderr 1>&2 )
stdout
stderr

Now, redirect stdout to "so", and stderr to
"se" (and look at those files):

dyi # ( echo stdout ; echo stderr 1>&2 ) > so 2> se
dyi # cat so
stdout
dyi # cat se
stderr

The files got their data, but there's nothing
left to be sent to the terminal (or into a
pipeline). Piping nothing into "wc" will
give you "0 0 0".

You could use "tee" to copy stdout into a
file, while passing it along in stdout. For
example:

dyi # ( echo stdout ; echo stderr 1>&2 ) 2> se | tee so
stdout
dyi # cat so
stdout
dyi # cat se
stderr

The files still get their data, but stdout
(here, "stdout") continues along to the
terminal, too. _This_ can be piped into
another program (like "wc"):

dyi # ( echo stdout ; echo stderr 1>&2 ) 2> se | tee so | wc
1 1 7

man tee

Look for "-a", which you'll probably want to
use (in place of ">>").
sekar sundaram
Honored Contributor

Re: output redirection and wc

thanks Steve, lot of info, good learning...
i found one small temporary (TEMP) idea, let me know this is good or not...here it is:

root@OVO> more agent-redeployment.sh
COUNT=1
for NODE in $( do

echo date is `date` >>HPDev.policies
echo Node Name - $NODE >>HPDev.policies
echo Server Count is ${COUNT} >>HPDev.policies
ovdeploy -cmd "opctemplate -list" -node $NODE >TEMPFILE 2>>HPDev.Policies.error
cat TEMPFILE >>HPDev.policies
wc TEMPFILE >>HPDev.policies
sleep 10
COUNT=${COUNT} + 1
done

1. this TEMPFILE idea is fine or not?
2. this COUNT=${COUNT} + 1 not works. like C++, do we have COUNT++ in shell scripting?
Dennis Handly
Acclaimed Contributor

Re: output redirection and wc

>1. this TEMPFILE idea is fine or not?

Yes it will work. Make sure you clean it up and not have more than one use of the script at a time. Otherwise you need to make it unique by adding .$$

>2. this COUNT=${COUNT} + 1 not works. like C++, do we have COUNT++ in shell scripting?

No, a real shell says ++/-- are not supported. Use:
(( count += 1 ))