Operating System - Linux
1753414 Members
7242 Online
108793 Solutions
New Discussion юеВ

Redirecting standard error within a for loop

 
SOLVED
Go to solution
Travis Harp_1
Advisor

Redirecting standard error within a for loop

Hello all,
I'm trying to get a little for loop working that will quickly show me disks not in a volume group thus likely available for use.
Here is what I'm trying to do:
for i in `ioscan -fnC disk | grep /dev/dsk| awk '{print $1}'`
do
pvdisplay $i | grep Cannot | more
done

Since the output that I'm looking for is an error from pvdisplay "pvdisplay: Cannot display physical volume "/dev/dsk/c36t1d5"." it is not being piped to more.
Putting the standard 2>&1 after the grep within the for loop isn't working and the only solution I've found is to make this a script and redirect it on the command line after the script.
I'm really wanting something I can type in directly on the command line rather than going through the process of making a script on each server I need this info on.
Can this be done?

Thanks,
Travis
5 REPLIES 5
OldSchool
Honored Contributor

Re: Redirecting standard error within a for loop

"Since the output that I'm looking for is an error from pvdisplay "pvdisplay:..."

If that's true, redirect the error from pvdisplay to stdout.

pvdisplay $1 2>&1 | grep .....

outta get it
Bob E Campbell
Honored Contributor
Solution

Re: Redirecting standard error within a for loop

I am assuming that you do not want stderr to also go through the grep. Many ways to do this, but I would try:

for i in `ioscan -fnC disk | grep /dev/dsk| awk '{print $1}'`
do
pvdisplay $i | grep Cannot
done 2>&1 | more
Patrick Wallek
Honored Contributor

Re: Redirecting standard error within a for loop

Try this:

for i in $(ioscan -fnC disk | grep /dev/dsk| awk '{print $1}')
do
pvdisplay ${i} > /dev/null 2>&1
[ $? = 0 ] && echo "${i} disk in use" || echo "${i} disk not in use"
done


First, notice I replaced you back-ticks (the ``) with the $() construct. That is much easier to read.

Now what this is doing is doing a pvdisplay and redirecting all output to /dev/null. The line after 'pvdisplay' if checking the pvdisplay return code (the $?). If the return code is 0, then pvdisplay was successful and the disk is likely in use. If the return code is not 0, then the disk is probably not in use.

Patrick Wallek
Honored Contributor

Re: Redirecting standard error within a for loop

By the way, you would be better off using 'ioscan -kfnC disk' rather than '-fnC disk'. Adding the '-k' tells ioscan to use what the kernel thinks is there and so it doesn't have to rescan your entire system. The ioscan will be much faster this way.
James R. Ferguson
Acclaimed Contributor

Re: Redirecting standard error within a for loop

Hi Travis:

You certainly don't need to use 'grep' AND 'awk' to match patterns! That's a waste of a process.

# ioscan -kfnC disk|awk '/dev\/dsk\// {print $1}'

...will return the disk device files.

Then, as Patrick suggested, use the return code from 'pvdisplay' to discern whether or not you have a "valid" device --- zero for success; non-zero for failure.

Regards!

...JRF...