From owner-freebsd-questions@FreeBSD.ORG Sat Aug 18 23:08:10 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 4305616A41A for ; Sat, 18 Aug 2007 23:08:10 +0000 (UTC) (envelope-from mail@chdevelopment.se) Received: from av8-1-sn3.vrr.skanova.net (av8-1-sn3.vrr.skanova.net [81.228.9.183]) by mx1.freebsd.org (Postfix) with ESMTP id 02B9B13C45B for ; Sat, 18 Aug 2007 23:08:09 +0000 (UTC) (envelope-from mail@chdevelopment.se) Received: by av8-1-sn3.vrr.skanova.net (Postfix, from userid 502) id 704D138258; Sun, 19 Aug 2007 01:08:08 +0200 (CEST) Received: from smtp3-1-sn3.vrr.skanova.net (smtp3-1-sn3.vrr.skanova.net [81.228.9.101]) by av8-1-sn3.vrr.skanova.net (Postfix) with ESMTP id 412A038047; Sun, 19 Aug 2007 01:08:08 +0200 (CEST) Received: from melissa.chdevelopment.se (90-227-26-163-no68.tbcn.telia.com [90.227.26.163]) by smtp3-1-sn3.vrr.skanova.net (Postfix) with ESMTP id 0696D37E45; Sun, 19 Aug 2007 01:08:07 +0200 (CEST) Message-ID: <46C77BD7.1080609@chdevelopment.se> Date: Sun, 19 Aug 2007 01:08:07 +0200 From: Christer Hermansson User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.8.1.6) Gecko/20070811 SeaMonkey/1.1.4 MIME-Version: 1.0 To: freebsd-questions@freebsd.org References: <46C726A8.9010404@chdevelopment.se> <6.0.0.22.2.20070818130942.02634918@mail.computinginnovations.com> In-Reply-To: <6.0.0.22.2.20070818130942.02634918@mail.computinginnovations.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: Derek Ragona 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: Sat, 18 Aug 2007 23:08:10 -0000 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 -- Christer Hermansson