I am writing a shell script.  I have a file that looks like this.

#some comments
22  ssh
25  smpt
80  http

And I want to iterate over the lines putting the first column into
var1 and the  second column into var2.  

I thought this would work:

#!/bin/sh

export INFILE=/etc/iptables/tcp.accept.dest.ports
if [ -f $INFILE ]; then
    #get the lines, ignoring empties and comments
    lines=`cat $INFILE | grep -v "^[[:space:]]*#" | grep -v '^[[:space:]]*$' 
2>/dev/null`
    for line in $lines; do
        col1=`echo $line | awk '{print $1}'`
        col2=`echo $line | awk '{print $2}'`
        echo "column 1 is $col1 and column 2 is $col2"
    done
else
    echo "Can't find $INFILE"
fi


But it doesn't behave as expected.

I know I could do it easily in perl or python, but I am trying to
limit myself to the bare bones UNIX utilities here.

Ideally the script could handle the case where var2 is empty,
something like

if (not empty var2)
  do something
else
  do the default this
fi

Suggestions?
John

Reply via email to