From owner-freebsd-questions@FreeBSD.ORG Sat Aug 18 18:13:18 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 2FA0216A418 for ; Sat, 18 Aug 2007 18:13:18 +0000 (UTC) (envelope-from derek@computinginnovations.com) Received: from betty.computinginnovations.com (mail.computinginnovations.com [64.81.227.250]) by mx1.freebsd.org (Postfix) with ESMTP id C558513C46A for ; Sat, 18 Aug 2007 18:13:17 +0000 (UTC) (envelope-from derek@computinginnovations.com) Received: from p28.computinginnovations.com (dhcp-10-20-30-100.computinginnovations.com [10.20.30.100]) (authenticated bits=0) by betty.computinginnovations.com (8.13.8/8.12.11) with ESMTP id l7IICvxD028365; Sat, 18 Aug 2007 13:12:58 -0500 (CDT) Message-Id: <6.0.0.22.2.20070818130942.02634918@mail.computinginnovations.com> X-Sender: derek@mail.computinginnovations.com X-Mailer: QUALCOMM Windows Eudora Version 6.0.0.22 Date: Sat, 18 Aug 2007 13:12:23 -0500 To: Christer Hermansson , freebsd-questions@freebsd.org From: Derek Ragona In-Reply-To: <46C726A8.9010404@chdevelopment.se> References: <46C726A8.9010404@chdevelopment.se> Mime-Version: 1.0 X-ComputingInnovations-MailScanner-Information: Please contact the ISP for more information X-ComputingInnovations-MailScanner: Found to be clean X-ComputingInnovations-MailScanner-From: derek@computinginnovations.com X-Spam-Status: No Content-Type: text/plain; charset="us-ascii"; format=flowed X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: 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 18:13:18 -0000 At 12:04 PM 8/18/2007, Christer Hermansson wrote: >Hi. > >I'm trying to use regular expressions inside a shell script (/bin/sh) on >my freebsd box and can't get it to work so I searched the web and found >http://regexlib.com/RETester.aspx > >On this webpage I could test my pattern "^[A-Za-z0-9_-]+$" and everything >was fine, did exactly what I wanted to do, check that a string only >contains some combination of the characters A-Z, a-z, 0-9, hyphen - and >underscore _. > >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. > >Is there anyone who has some advice about how to get regular expressions >to work in FreeBSD shell script ? > >-- > >Christer Hermansson 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 -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. MailScanner thanks transtec Computers for their support.