From owner-freebsd-questions@FreeBSD.ORG Thu Aug 14 14:26:40 2014 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 6536E6BB for ; Thu, 14 Aug 2014 14:26:40 +0000 (UTC) Received: from blue.qeng-ho.org (blue.qeng-ho.org [217.155.128.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id F0E9F2D08 for ; Thu, 14 Aug 2014 14:26:39 +0000 (UTC) Received: from fileserver.home.qeng-ho.org (localhost [127.0.0.1]) by fileserver.home.qeng-ho.org (8.14.7/8.14.5) with ESMTP id s7EEQUW7004818; Thu, 14 Aug 2014 15:26:30 +0100 (BST) (envelope-from freebsd@qeng-ho.org) Message-ID: <53ECC716.2020106@qeng-ho.org> Date: Thu, 14 Aug 2014 15:26:30 +0100 From: Arthur Chance User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:31.0) Gecko/20100101 Thunderbird/31.0 MIME-Version: 1.0 To: Rick Miller , FreeBSD Questions Subject: Re: /bin/sh script not behaving as expected References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Aug 2014 14:26:40 -0000 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"