home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!swrinde!zaphod.mps.ohio-state.edu!uwm.edu!spool.mu.edu!yale.edu!ira.uka.de!math.fu-berlin.de!news.netmbx.de!Germany.EU.net!mcsun!sunic!palantir.p.tvt.se!infolog!mich
- From: mich@lin.infolog.se (Thomas Michanek)
- Subject: Re: How to recognize a csh var as a number?
- Message-ID: <1993Jan28.122255.8354@infolog.se>
- Sender: news@infolog.se
- Organization: Infologics, Linkoping, Sweden
- References: <1jn3t3INN88b@haydn.crhc.uiuc.edu> <1993Jan26.231002.165645@dstos3.dsto.gov.au> <1993Jan27.111224.1263@mfltd.co.uk>
- Date: Thu, 28 Jan 1993 12:22:55 GMT
- Lines: 64
-
- In article <1993Jan27.111224.1263@mfltd.co.uk> dnh@mfltd.co.uk (Des Herriott) writes:
- >
- >In article <1993Jan26.231002.165645@dstos3.dsto.gov.au>, sct@tumtum (Shaun Troedson) writes:
- >> Timothy Tsai (ttsai@crhc.uiuc.edu) wrote:
- >> : I am reading string values into a csh variable. Depending on whether each
- >> : string represents a number or not, I want to do certain things. How do I
- >> : test to see if the variable string is a number or not?
- >>
- >
- >The above problem is simple if you use sh or (and undoubtedly bash too, though
- >I haven't used it):
- >
- >case $variable in
- > [0-9][0-9]*)
- > # it's a number
- > break;;
- > *)
- > # it's not a number
- >esac
- >
- >See? No need to fork a new shell, no need to do any output redirection. It's
- >all done by one process. I can't offhand think of a way of doing that in csh
- >(and I used csh for 3 years before I saw the light :-)
- >
-
- OK, I'm not a sh programmer, so please don't flame me if I'm wrong, but...
-
- 1. The above case doesn't work for single-digit numbers; it should read
- case $variable in
- [0-9]*)
-
- 2. This is just as easy in csh (at least it works for me):
- switch ($variable)
- case [0-9]*:
- # it's a number
- breaksw
- default:
- # it's not a number
- endsw
-
- 3. Neither of the above warns for numbers followed by non-digit characters.
- In csh you'll have to do:
- if (`echo $variable | egrep '^[0-9]+$'` != "") then
-
- This forks a new shell, but it solves the problem correctly. It doesn't
- use output redirection, which this one does:
- echo $variable | egrep '^[0-9]+$' >& /dev/null
- if ($status == 0) then
-
- >--
- >Des Herriott, / "...and I hate, and I hate, and I hate,
- >Micro Focus, Newbury. / elevator music..."
- >+44 (0635) 565354 /
- >dnh@mfltd.co.uk / -- Tori Amos, Little Earthquakes.
-
- Correct me if I'm wrong...
-
- .- - - - - - - - - - - -. _ _ _ ___ _ _ _ _ _ _ _ .- - - - - - - -.
- | Thomas Michanek | | |\ | |_ / \ | / \ / _ | / `|_` | Dial +46 13 |
- | Infologics Linkoping | | | \| | \_/ |_,\_/ \_| | \_,._| | 210060 Phone |
- | email:Thomas.Michanek | _ | 210068 Direct|
- | @lin.infolog.se | L I N K O P I N G - S W E D E N | 210155 Fax |
- `- - - - - - - - - - - -' `- - - - - - - -'
-
-