From owner-freebsd-questions@FreeBSD.ORG Thu May 16 16:48:28 2013 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 72E6C856; Thu, 16 May 2013 16:48:28 +0000 (UTC) (envelope-from Devin.Teske@fisglobal.com) Received: from mx1.fisglobal.com (mx1.fisglobal.com [199.200.24.190]) by mx1.freebsd.org (Postfix) with ESMTP id BF6F52D7; Thu, 16 May 2013 16:48:26 +0000 (UTC) Received: from smtp.fisglobal.com ([10.132.206.16]) by ltcfislmsgpa03.fnfis.com (8.14.5/8.14.5) with ESMTP id r4GGmQlo012311 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Thu, 16 May 2013 11:48:26 -0500 Received: from LTCFISWMSGMB26.FNFIS.com ([10.132.99.18]) by LTCFISWMSGHT05.FNFIS.com ([10.132.206.16]) with mapi id 14.02.0309.002; Thu, 16 May 2013 11:48:25 -0500 From: "Teske, Devin" To: Devin Teske Subject: Re: check variable content size in sh script Thread-Topic: check variable content size in sh script Thread-Index: AQHOUkc8OBFwBehJxk+zMS+kIvSMRZkIQzQAgAAKzgCAAAXigIAABbyA Date: Thu, 16 May 2013 16:48:26 +0000 Message-ID: <13CA24D6AB415D428143D44749F57D7201F4D5A7@ltcfiswmsgmb26> References: <5194F65F.6080503@a1poweruser.com> <5194FB0A.9090400@tundraware.com> <13CA24D6AB415D428143D44749F57D7201F4D41F@ltcfiswmsgmb26> <13CA24D6AB415D428143D44749F57D7201F4D4C9@ltcfiswmsgmb26> In-Reply-To: <13CA24D6AB415D428143D44749F57D7201F4D4C9@ltcfiswmsgmb26> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.132.253.126] MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.10.8626, 1.0.431, 0.0.0000 definitions=2013-05-16_04:2013-05-16,2013-05-16,1970-01-01 signatures=0 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.14 Cc: Tim Daneliuk , FreeBSD Questions X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: Devin Teske List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 May 2013 16:48:28 -0000 On May 16, 2013, at 9:27 AM, Teske, Devin wrote: On May 16, 2013, at 9:06 AM, Teske, Devin wrote: On May 16, 2013, at 8:28 AM, Tim Daneliuk wrote: On 05/16/2013 10:08 AM, Joe wrote: Hello Have script that has max size on content in a variable. How to code size less than 51 characters? FOO=3D"Some string you want to check length of" FOOLEN=3D`echo $FOO | wc | awk '{print $3}'` Uh, without forking to 2 separate programs=85 FOOLEN=3D${#FOO} You can then use $FOOLEN in a conditional. However, if the OP wanted to actually truncate $FOO to 51 characters: NEWFOO=3D$( echo "$FOO" | awk -v max=3D51 '{print substr($0,0,max)}' ) However, if you want to handle the case of $FOO containing newlines (and yo= u want the newline to count toward the max), then this instead would do the= trick: NEWFOO=3D$( echo "$FOO" | awk -v max=3D51 ' { len =3D length($0) max -=3D len print substr($0,0,(max > 0 ? len : max + len)) if ( max < 0 ) exit max-- }' ) For fun, I decided to expand on the solution I provided immediately above= =85 turning it into a function that you might be a little more familiar wit= h: snprintf() { local __var_to_set=3D"$1" __size=3D"$2" shift 2 # var_to_set/size eval "$__var_to_set"=3D\$\( printf \"\$@\" \| awk -v max=3D\"\$__siz= e\" \'' { len =3D length($0) max -=3D len print substr($0,0,(max > 0 ? len : max + len)) if ( max < 0 ) exit max-- }'\' \) } Example usage: FOO=3D$( printf "abc\n123\n" ) snprintf NEWFOO 6 "%s" "$FOO" echo "NEWFOO=3D[$NEWFOO] len=3D[${#NEWFOO}]" Produces: NEWFOO=3D[abc 12] len=3D[6] Hopefully this should help some folks. I figured I'd help as many folks as I can=85 http://svnweb.freebsd.org/base?view=3Drevision&revision=3D250701 Added it to my string processing library. Lots of other useful functions in= there. -- Cheers, Devin $NEWFOO, even if multi-line, will be limited to 51-bytes (adjust max=3D51 a= ccordingly for other desired-lengths). Newlines are preserved. Last, but not least, if you want to be able to handle multi-line values but= only want to return the first line up-to N bytes (using 51 as the OP used): NEWFOO=3D$( echo "$FOO" | awk -v max=3D51 '{ print substr($0,0,max); exit }= ' ) If $FOO had multiple lines, $NEWFOO will have only the first line (and it w= ill be truncated to 51 bytes or less). -- Devin _____________ The information contained in this message is proprietary and/or confidentia= l. If you are not the intended recipient, please: (i) delete the message an= d all copies; (ii) do not disclose, distribute or use the message in any ma= nner; and (iii) notify the sender immediately. In addition, please be aware= that any message addressed to our domain is subject to archiving and revie= w by persons other than the intended recipient. Thank you.