From owner-freebsd-questions@FreeBSD.ORG Mon Aug 20 17:03:38 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id A4D0A16A419 for ; Mon, 20 Aug 2007 17:03:38 +0000 (UTC) (envelope-from lreid@cs.okstate.edu) Received: from a.cs.okstate.edu (a.cs.okstate.edu [139.78.113.1]) by mx1.freebsd.org (Postfix) with ESMTP id 831EB13C48A for ; Mon, 20 Aug 2007 17:03:38 +0000 (UTC) (envelope-from lreid@cs.okstate.edu) Received: from [172.18.0.137] (unknown [70.168.226.150]) by a.cs.okstate.edu (Postfix) with ESMTP id 09C28A06AA; Mon, 20 Aug 2007 12:03:37 -0500 (CDT) Message-ID: <46C9C95C.8030606@cs.okstate.edu> Date: Mon, 20 Aug 2007 12:03:24 -0500 From: Reid Linnemann User-Agent: Thunderbird 2.0.0.6 (X11/20070809) MIME-Version: 1.0 To: Christer Hermansson References: <46C726A8.9010404@chdevelopment.se> <6.0.0.22.2.20070818130942.02634918@mail.computinginnovations.com> <46C77BD7.1080609@chdevelopment.se> <46C9C846.7060100@cs.okstate.edu> In-Reply-To: <46C9C846.7060100@cs.okstate.edu> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: freebsd-questions@freebsd.org Subject: Re: Regular expressions X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Aug 2007 17:03:38 -0000 Written by Reid Linnemann on 08/20/07 11:58>> > Written by Christer Hermansson on 08/18/07 18:08>> >> Derek Ragona wrote: >>> At 12:04 PM 8/18/2007, Christer Hermansson wrote: >>>> I also found some basic example at >>>> http://www.grymoire.com/Unix/Sh.html#uh-88 : >>>> >>>> --------8<--------8<--------8<--------8<--------8<-------- >>>> >>>> #!/bin/sh >>>> >>>> echo "Type in a number" >>>> read ans >>>> number=`expr "$ans" : "([0-9]*)"` >>>> if [ "$number" != "$ans" ]; then >>>> echo "Not a number" >>>> elif [ "$number" -eq 0 ]; then >>>> echo "Nothing was typed" >>>> else >>>> echo "$number is a fine number" >>>> fi >>>> >>>> --------8<--------8<--------8<--------8<--------8<-------- >>>> >>>> The above example doesn't work on my freebsd box. Maybe I need to >>>> update my system, sitting with 6.0R which never been updated. >>>> >>> >>> You have a syntax error using expr. Do a man on expr for more >>> details but if you change that line from: >>> number=`expr "$ans" : "([0-9]*)"` >>> to: >>> number=`expr "$ans" : "\([0-9]*\)"` >>> >>> You will get the desired results. >>> >>> Also when debugging scripts remember to add: >>> set -x >>> to your script on the second line, and see what the script lines are >>> actually doing. >>> >>> -Derek >>> >> Thanks Derek ! Now both the example and my own code works for me. I >> changed my code from "^[A-Za-z0-9_-]+$" to "\([A-Za-z0-9_-]*\)" It >> seems that FreeBSD's expr want some different syntax than the webbased >> test tool at http://regexlib.com/RETester.aspx >> > > No, your expression is double quoted, which means the shell will expand > it before passing it to expr. Parens are expanded by shells, they > manipulate the order of operations (i.e. 'echo 1 || echo 2 && echo 3' > vs. '(echo 1 || echo 2) && echo 3'). As a result, you must escape the > parens or the shell will gobble them up. Disregard that, I am a moron. :/