In the following snippet, $2 in awk is returning empty. What am I doing wrong? I am trying to find the difference between MAX and MIN.
#!/bin/ksh
if [ $# -ne 1 ]; then
echo "Usage: sh `basename ${0}` filename";
exit 1;
fi
if [ ! -s ${1} ]; then
echo "file doesn't exist or is empty";
exit 1;
fi
sed -e '/^$/d' -e '/^#/d' ${1} |
awk -F'=' '
BEGIN {
MIN=$2; MAX=$2; print MIN;
}
{
if ( $2 > MAX )
{
MAX = $2;
}
else if ( $2 < MIN )
{
MIN = $2;
}
}
END {
DIFF=MAX-MIN; print "DIFF:" DIFF;
}
'
However, this is working fine. Why doesn't $2 work in the BEGIN section?
#!/bin/ksh
if [ $# -ne 1 ]; then
echo "Usage: sh `basename ${0}` filename";
exit 1;
fi
if [ ! -s ${1} ]; then
echo "file doesn't exist or is empty";
exit 1;
fi
sed -e '/^$/d' -e '/^#/d' ${1} |
awk -F'=' '
{
if ( MAX == "" || MIN == "" )
{
MAX = MIN = $2;
}
else
{
if ( $2 > MAX )
{
MAX = $2;
}
else if ( $2 < MIN )
{
MIN = $2;
}
}
}
END {
DIFF=MAX-MIN; print "DIFF:" DIFF;
}
'