help with not so complex if-else in bourne shell scripting

Hello. I'm trying to get a not-so complex if-else to work on my FreeBSD box but I'm getting an error with the second part of the condition. Basically, this is what I'm trying to do:
Code:
    if not file-exists or (file-exists and string exists in file) then
     do this
    else
     do something else

This is the actual code I'm using:
Code:
    if [ ! -f /boot/loader.conf ] || [[ -f /boot/loader.conf ] && ! grep -Fqx "zfs_l
    oad" /boot/loader.conf ]; then
                    echo "found"
            else
                    echo "not found"
    fi

It gives me an error about "[[". I tried adding/removing brackets to no avail. I' I've also searched the net for similar examples but the ones I've seen are very simplistic (i.e. if var=value then do this). I could separate the conditions into two "ifs" but I think it can be done in one and I want to know "advance" if-else in Bourne as well. :)

Any help would be greatly appreciated.

Thanks :)
 
Re: help with not so complex if-else in bourne shell scripti

test(1) has provisions for and and or operations in the expression with -a and -o. That would be the advanced way to do this, although, like with most shell stuff, it's horribly ugly to write and difficult to maintain. Two ifs make more sense here. The -Fqx flags don't make sense to me, so I've changed them in the following example.
Code:
conf="/boot/loader.conf"
if [ ! -f $conf ]; then
  echo "no $conf file"
else
  grep -q "zfs_load" $conf  
  if [ $? eq 0 ]; then
    echo "$conf present and "zfs_load" in it"
  fi
fi

The tests are... somewhat questionable. The lack of boot.conf does not mean that the ZFS module has been loaded. The real indicator would be the output of kldstat, or maybe a sysctl.

But if those tests are really what you need, the exit status of grep(1) would do the same thing (untested):
Code:
conf="/boot/loader.conf"
grep -q "zfs_load" $conf
if [ $? eq 0 ]; then
  echo "$conf present and zfs_load in it"
else
  echo "$conf not present or present but does not contain zfs_load"
fi
 
Re: help with not so complex if-else in bourne shell scripti

The bracket is actually a command (test(1)) and not a syntactic element in the sh(1) grammar and can not be used for grouping. From the manual page:

Code:
Grouping Commands Together
     Commands may be grouped by	writing	either
	   (list)
     or
	   { list; }
 
Back
Top