سؤال

I'm trying to get a not-so complex if-else to work on my FreeBSD box but I'm getting error with the second part of the condition.

basically, this is what I'm trying to do

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

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'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 2 "ifs" but I think it can be done in 1 and I'm want to know "advance" if-else in bourne as well. :)

Any help would be greatly appreciated.

thanks :)

هل كانت مفيدة؟

المحلول

Use { ... } for grouping without the overhead and side-effects of a subshell (as created by ( ... )). [[ ]] is a different syntax, only available in ksh derivatives such as bash, which replaces [ ... ] with a less-error-prone alternative; it isn't available in baseline POSIX shells.

[ ! -f /boot/loader.conf ] || \
  { [ -f /boot/loader.conf ] && ! grep -Fqx "zfs_load" /boot/loader.conf; }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top