From owner-freebsd-questions@freebsd.org Sat Sep 9 16:38:17 2017 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 1B335E204C3 for ; Sat, 9 Sep 2017 16:38:17 +0000 (UTC) (envelope-from freebsd@qeng-ho.org) Received: from bede.home.qeng-ho.org (bede.qeng-ho.org [217.155.128.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "fileserver.home.qeng-ho.org", Issuer "fileserver.home.qeng-ho.org" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id B16C26F130 for ; Sat, 9 Sep 2017 16:38:15 +0000 (UTC) (envelope-from freebsd@qeng-ho.org) Received: from arthur.home.qeng-ho.org (arthur.home.qeng-ho.org [172.23.1.2]) by bede.home.qeng-ho.org (8.15.2/8.15.2) with ESMTP id v89Gc50r038702; Sat, 9 Sep 2017 17:38:06 +0100 (BST) (envelope-from freebsd@qeng-ho.org) Subject: Re: script code for end-line To: Ernie Luzar , Yuri Pankov Cc: Polytropon , "freebsd-questions@freebsd.org" References: <59B332A3.1000205@gmail.com> <20170909030257.d2718c00.freebsd@edvax.de> <59B4087B.2070005@gmail.com> From: Arthur Chance Message-ID: Date: Sat, 9 Sep 2017 17:38:05 +0100 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: <59B4087B.2070005@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 8bit X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 09 Sep 2017 16:38:17 -0000 On 09/09/2017 16:27, Ernie Luzar wrote: > Yuri Pankov wrote: >> On Sat, 9 Sep 2017 03:02:57 +0200, Polytropon wrote: >>> On Fri, 08 Sep 2017 20:15:31 -0400, Ernie Luzar wrote: >>>> >>>> I have a file that has blank lines with ^M in position one. >>>> >>>> I have this  if [ "$end-line" = "^M"]; then >>>> >>>> >>>> Is that the correct way to code that between the quotes? >>> >>> That will only match the literal string ^M (^ and M). >>> String evaluation and comparison at this low level >>> isn't a native skill of sh. There is a way of encoding >>> characters as octal values, such as \015 for \r, which >>> equals ^M and 0x0D, but /bin/test (which is [) can only >>> compare strings. >>> >>> Here is a terrible workaround (not tested): >>> >>> if [ `echo ${end-line} | od -x | head -n 1 | awk '{ print $2 }'` = >>> "000d" ]; then >>>     ... do something ... >>> fi >>> >>> Check if there is already a tool for what you're trying >>> to accomplish (e. g., tr, sed, recode, iconv). ;-) >> >> Actually, you can insert real ^M characters and /bin/test should be >> able to handle them - press ctrl+V ctrl+M. >> . >> > > I read the man page on the test command and did not come away with the > syntax to use in a script. An example showing usage inside of the "if" > statement sure would be more helpful to understand how it works. As we're talking Bourne shell, "man sh" and look for the $'...' string form. Dollar-Single Quotes Enclosing characters between $' and ' preserves the literal mean‐ ing of all characters except backslashes and single quotes. A backslash introduces a C-style escape sequence: -- An amusing coincidence: log2(58) = 5.858 (to 0.0003% accuracy).