ksh regexp ?

ksh regexp ?

Post by Phil R Lawrenc » Sat, 30 May 1998 04:00:00



Hello,

I normally program in Perl, today I'm in ksh.  Having some trouble...

I want to assign my second parameter for my program based on whether it is a
number or not.

I tried:
FILENM=$1
if [[ -z "$2" || "$2" != /^\d+$/ ]] then
    QUEUE=$2
    WIDTH=$3
    PGLINES=$4
else
    WIDTH=$2
    PGLINES=$3
fi

Hopefully you can see I thought I'd use a regexp to determine if its a number or
not.  This didn't work.  Probably because I'm guessing on the regexp format for
ksh.  Any help, or perhaps a better way to solve my problem?

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Phil R Lawrence               phone: 610-758-3051

194 Lehigh University Computing Center
E.W. Fairchild - Martindale, Bldg. 8B
Bethlehem, PA  18018
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 
 
 

ksh regexp ?

Post by Christian Bauernfei » Sat, 30 May 1998 04:00:00




Quote:> Hello,

> Hopefully you can see I thought I'd use a regexp to determine if its a number or
> not.  This didn't work.  Probably because I'm guessing on the regexp format for
> ksh.  Any help, or perhaps a better way to solve my problem?

ksh doesn't use regexps, but glob patterns for pattern matching.
(The ones where ? is . and * is .*)
Try using [0-9]* as your pattern. Unless you know exactly how many
digits you are dealing with, this is the closest you will get.

Christian
--
Christian Bauernfeind
Not speaking for Siemens
Not even working for IBM


 
 
 

ksh regexp ?

Post by Douglas Wilso » Sun, 31 May 1998 04:00:00



> Hello,

> I normally program in Perl, today I'm in ksh.  Having some trouble...

> I want to assign my second parameter for my program based on whether it is a
> number or not.

> I tried:
> FILENM=$1
> if [[ -z "$2" || "$2" != /^\d+$/ ]] then

try (no quotes necessary inside [[]]):
if [[ -z $2 || $2 != +([0-9]) ]]; then
or even:
if [[ -z $2 || $2 != +([[:digit:]]) ]]; then]

Hope that helps,
Douglas Wilson

 
 
 

ksh regexp ?

Post by brian hil » Wed, 03 Jun 1998 04:00:00



: I want to assign my second parameter for my program based on whether it is a
: number or not.

# isnum returns True if its argument is a valid number.
# The first pattern requires a digit before the decimal
# point, and the second after the decimal point.
function isnum # string
{
    case $1 in
    ?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) )
        return 0;;
    ?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])) )
        return 0;;
    *) return 1;;
    esac
    # Not reached.

Quote:}

if isnum $2
then    print it is a number
else    print it is not a number
fi

-Brian

 
 
 

ksh regexp ?

Post by brian hil » Wed, 03 Jun 1998 04:00:00



: ...
: if [[ -z $2 || $2 != +([[:digit:]]) ]]; then]

That's a ksh93 extension to comply with the POSIX standard.

-Brian

 
 
 

1. ksh: 'unset' variables in 'regexp-like' fashion?

The problem is this: a script generates (very) many
variables that have a certain pattern to them, like this:

   $ AB_0_0=1
   $ AB_0_1=2

At some point I need to 'unset' all of them:

   $ unset AB_0_0 AB_0_1

This is practical only for a few of them. Is there a way
to unset all of them in some simple fashion, like

   $ unset AB*  

I know the above (which is meant to be 'regexp-like')
doesn't work, but this is the idea I am after.

If the variables were all exported, one could think of
something like (not pretty):

   $ env | grep '^AB' | while read var
                        do unset ${var%%=*}
                        done

but my variables are not exported (so 'env' doesn't output
them).

Any idea?

---Zdenek

--
=========================================================

VNET: 557-4844                      Silicon Graphics SA
Tel : +41-22-710.4844       18, Ave. Louis Casai, 1209 Geneva
Fax : +41-22-710.4860                   Switzerland

2. ddd debugger ?

3. root has /bin/ksh while users have /usr/bin/ksh

4. char _depends_on[] = " ";

5. KSH fixed (was Re: KSH is REALLY BROKEN in Slackware!)

6. Help me. Have to infin Motif 2.0

7. 2.6 /sbin/ksh vs /bin/ksh

8. user last login date

9. ksh - Are there semaphores in ksh? Concurrency issues...

10. What incompatibilities between ksh 12/28/93e and ksh 11/16/88h

11. ksh head ache - here doc within a ksh function

12. ksh script problem: pwd works differently for ksh then linux binary file

13. Urgent for KSH Gurus : matching source and target lists to create new one(ksh)