/bin/sh script not behaving as expected

Rick Miller vmiller at hostileadmin.com
Thu Aug 14 14:36:51 UTC 2014


On Thu, Aug 14, 2014 at 10:26 AM, Arthur Chance <freebsd at qeng-ho.org> wrote:

> 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;
>>
>>
>> [ snip ]
>>
>>
> 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"
>

Excellent, thanks!  My understanding of parameter expansion was flawed.  I
appreciate the info.

-- 
Take care
Rick Miller


More information about the freebsd-questions mailing list