/bin/sh script not behaving as expected

Rick Miller vmiller at hostileadmin.com
Thu Aug 14 14:13:55 UTC 2014

Hi all,

I have shell code whose purpose is to determine the first disk in the
system where FreeBSD is to be installed.  The code is not behaving as
expected and I’m hoping that fresh pairs of eyes might help me identify the

Here is the script along with an explanation of the implementation and
description of the problem:

#! /bin/sh

disks="da2 da1 da0";

for d in ${disks}; do
   if [ -z "${disk}" -o "${disk}" '>' "${d}" ]; then
      : ${disk:=${d}};

echo $disk;

# Given the input(s), $disks, the expected behavior of the above code is to
# set $disk to "da0" which ends up being the chosen disk to install FreeBSD.
# The for() loop iterates over $disks.  The if() statement tests the status
# $disk; If $disk is unset/null, $disk is set to $d.  If $disk is set, it
# compares the binary value of $disk to $d and should select the element
# the lower binary value.  In this particular case, it is expected to select
# da0 as the ultimate value for $disk, but in practice, it appears to set
# $disk to da2 instead.
# NOTE: This is on FreeBSD 10.  The code has been tested in bash as well,
# returns the same results.


Take care
Rick Miller

More information about the freebsd-questions mailing list