Date: Thu, 16 May 2013 16:06:50 +0000 From: "Teske, Devin" <Devin.Teske@fisglobal.com> To: Tim Daneliuk <tundra@tundraware.com> Cc: "<freebsd-questions@freebsd.org>" <freebsd-questions@freebsd.org> Subject: Re: check variable content size in sh script Message-ID: <13CA24D6AB415D428143D44749F57D7201F4D41F@ltcfiswmsgmb26> In-Reply-To: <5194FB0A.9090400@tundraware.com> References: <5194F65F.6080503@a1poweruser.com> <5194FB0A.9090400@tundraware.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On May 16, 2013, at 8:28 AM, Tim Daneliuk wrote: > On 05/16/2013 10:08 AM, Joe wrote: >> Hello >>=20 >> Have script that has max size on content in a variable. >> How to code size less than 51 characters? >>=20 >=20 > FOO=3D"Some string you want to check length of" > FOOLEN=3D`echo $FOO | wc | awk '{print $3}'` >=20 Uh, without forking to 2 separate programs=85 FOOLEN=3D${#FOO} > You can then use $FOOLEN in a conditional. >=20 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-- }' ) $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). --=20 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.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?13CA24D6AB415D428143D44749F57D7201F4D41F>