Strange if else

From: Dave Markham (dave.markham@fjserv.net)
Date: Thu Feb 01 2007 - 11:31:16 EST


Can anyone explain to me the following.

If i have the below code and hash out the else statement it works as it
should and it skips comments and blank lines in a config file and splits
a field shown in the example config file into 2. If there are not 2
fields then the script errors.

What i dont get is if i put the else statement in to try and catch a
file which is ONLY comments and blank lines ( thus not matching the
first if [ ] ) what it actually does is match anything and errors with
the else ERROR. So far a correct config im its hitting the else statement

Example config file

# This is a config file for the NovaB Remote backup script
# It should contain a list of servers and functions
# one per line and colon separated with only 2 fields
#
# e.g boxa:function
#
# Any more than 2 : separated fields will be ignored
boxa:function1
boxb:function2

boxc:function3:this_is_ignored

Code :-

config_file=$1
while read line
        do
                # Ignore comments or blank lines
                # '#' match Only seem to work with [[ ]] and no "
                # Investigate!! KSH faq
                if [[ $line != \#* ]] && [[ "$line" != "" ]];then
                        # Only return first 2 fields from
                        # config file so to satisfy below
                        # two variables
                        newline=$(echo "$line" | awk -F: '{print $1":"$2}')
                        REM_HOST=${newline%:*} # before first :
                        REM_FUNC=${newline#*:} # after first :
                        # 1 of REM_ vars is blank we have a problem
                        if [ ! "$REM_HOST" ] || [ ! "$REM_FUNC" ];then
                                echo "\n\tERROR: Parsing config file
[$config_file] Check formatting. Exiting\n"
                                exit 2
                        fi

                        #DEBUG
                        echo "host is [$REM_HOST], function is
[$REM_FUNC]" # Debug

                #else
                        # File must be just comments or blank lines
                        #echo "\n\tERROR: Empty or comment only config
file [$config_file]. Exiting\n"
                        #exit 2
                fi
done < $config_file

Results if else is commented out :-

$ ksh test2.ksh config
host is [boxa], function is [function1]
host is [boxb], function is [function2]
host is [boxc], function is [function3]

Result if else is NOT commented out :-

$ ksh test2.ksh config

        ERROR: Empty or comment only config file [config]. Exiting

$

I dont get it!
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 23:41:34 EDT