From owner-freebsd-hackers@FreeBSD.ORG Fri Nov 12 01:26:34 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 4652D1065672 for ; Fri, 12 Nov 2010 01:26:34 +0000 (UTC) (envelope-from dteske@vicor.com) Received: from postoffice.vicor.com (postoffice.vicor.com [69.26.56.53]) by mx1.freebsd.org (Postfix) with ESMTP id 1A9E88FC29 for ; Fri, 12 Nov 2010 01:26:33 +0000 (UTC) Received: from [208.206.78.30] (port=48741 helo=dt.vicor.com) by postoffice.vicor.com with esmtpsa (SSLv3:RC4-MD5:128) (Exim 4.71) (envelope-from ) id 1PGiPP-0002pI-Bc for freebsd-hackers@freebsd.org; Thu, 11 Nov 2010 17:26:33 -0800 From: Devin Teske To: freebsd-hackers@freebsd.org In-Reply-To: <1289506363.30235.113.camel@localhost.localdomain> References: <1289506363.30235.113.camel@localhost.localdomain> Content-Type: text/plain Organization: Vicor, Inc Date: Thu, 11 Nov 2010 17:26:31 -0800 Message-Id: <1289525191.30235.117.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 (2.0.2-41.el4) Content-Transfer-Encoding: 7bit X-Scan-Signature: 83be3b23014bddc0a2b81e211cffcda6 X-Scan-Host: postoffice.vicor.com Subject: Re: Spinner Function for Shell Scripts X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Nov 2010 01:26:34 -0000 One-line fix for a small bug (see below). On Thu, 2010-11-11 at 12:12 -0800, Devin Teske wrote: > Hi fellow hackers... I come with baring gifts! > > So, just as the subject-line says, ... here's an efficient and robust > spinner function compatible with many shells. > > But, before I get into the code, > > [snip] > > eval_spin() > { > local commands= > if [ $# -gt 0 ]; then > # Take commands from positional arguments > while [ $# -gt 0 ]; do > commands="$commands${commands:+ }'$1'" > shift 1 > done > else > # Take commands from standard input > while read -r LINE; do > commands="$commands > $LINE" > done > fi > [ "$commands" ] || return $SUCCESS > > # > # Execute the command w/ spinner > # > ( > eval "$commands" > /dev/null 2>&1 The above line should instead be: ( eval "$commands" ) > /dev/null 2>&1 To protect from things such as: eval_spin exit > echo $? > ) | ( > n=1 > spin="/-\\|" > DONE= > > echo -n " " > while [ ! "$DONE" ]; do > DONE=$( /bin/sh -c 'read -t 0 DONE; echo $DONE' ) > printf "\b%s" $( echo "$spin" | sed -e \ > "s/.\{0,$(( $n % ${#spin} ))\}\(.\).*/\1/" ) > n=$(( $n + 1 )) > done > printf "\b \b" > exit $DONE > ) > } > > [snip] -- Cheers, Devin Teske -> CONTACT INFORMATION <- Business Solutions Consultant II FIS - fisglobal.com 510-735-5650 Mobile 510-621-2038 Office 510-621-2020 Office Fax 909-477-4578 Home/Fax devin.teske@fisglobal.com -> LEGAL DISCLAIMER <- This message contains confidential and proprietary information of the sender, and is intended only for the person(s) to whom it is addressed. Any use, distribution, copying or disclosure by any other person is strictly prohibited. If you have received this message in error, please notify the e-mail sender immediately, and delete the original message without making a copy. -> END TRANSMISSION <-