Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If-then-else statement

Status
Not open for further replies.

h2hummer47

Technical User
Dec 11, 2002
7
US
My 'else' isn't working. I only want entire lines containing 'error' for servers with Veritas loaded. Where there are no error status when running vxdisk list it should print the comment. Help...

#!/bin/ksh
touch vxdisk_check && chmod 777 vxdisk_check
echo "Veritas Disk Failure Report"
echo "As of:" `date`
echo ""
echo ""
for host in `cat /home/dtalley/scripts/vxdisklist`
do
echo $host
rsh $host /usr/sbin/vxdisk list |
nawk
'{
if($0~/error/) print $0}
else {print "All is good"}
fi
}'
done
 
Hi,

I think I'd try to add some { like

'{
if($0 ~ /error/) {print $0}
else {print "All is good"}
fi
}'

or remove all

'{
if($0 ~ /error/) print $0
else print "All is good"
fi
}'

 
You have a "}" where it is not needed in the line:

if($0~/error/) print $0}

I think thing may work if that "}" is removed.

 
Not sure about nawk, however, in awk (and nawk should work the same) you don't have a fi to end an if; that is a shell syntax statement.

{
if($0~/error/) {
print $0
}
else
print "All is good."
}
 
by changing it to look like this
if($0~/error/) {print $0};
done
The printout is such:
denws2
c0t14d0s2 sliced - - error
blvuws2

With changing to this:
if($0~/error/) {print $0};
{print "All is good"}
}'
done

I get not only the 'error' line, I get "All is good" for every line that's in the vxdisk list.
 
The AWK syntax is condition { script }. Since you haven't specified a condition before your script it will match, and be executed, for every line of input.

Try something like this:

[tt]BEGIN { error=0 }
/error/ {
error=1
print
}
END {
if (error==0) {
print "All is well."
}
}'

The BEGIN and END are special conditions for scripts which are executed before and after the input respectively. The script after the /error/ condition will be executed for every line where $0 contains 'error'. Annihilannic.
 
FYI: Annihilannic
BEGIN { error=0 } is superfluos
awk initialize his vars to 0 || "", not 'c'.

-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Try it so. If-Then-Else

if($0~/error/)then print $0
else print "All is good"
fi
endif

Moreno
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top