/bin/sh script not behaving as expected

Arthur Chance freebsd at qeng-ho.org
Thu Aug 14 14:26:40 UTC 2014


On 14/08/2014 15:13, Rick Miller wrote:
> 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
> problem.
>
> 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}};
>     fi
> done
>
>
> 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
> of
> # $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
> with
> # 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,
> which
> # returns the same results.
>
> TIA
>

Your problem is in

        : ${disk:=${d}};

 From man sh

${parameter:=word}
        Assign Default Values.  If parameter is unset or null, the expan‐
        sion of word is assigned to parameter.  In all cases, the final
        value of parameter is substituted.  Quoting inside word does not
        prevent field splitting or pathname expansion.  Only variables,
        not positional parameters or special parameters, can be assigned
        in this way.

Once $disk has been set this form won't reset it.

You should have

	disk="$d"




More information about the freebsd-questions mailing list