Solved

number validation in shell scripts

Posted on 2000-02-15
6
347 Views
Last Modified: 2013-12-26
How to validate whether the input from keyboard is an integer or not?
0
Comment
Question by:prasen120998
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2526029
which shell?
0
 
LVL 84

Expert Comment

by:ozo
ID: 2526064
and would you consider inputs like "-1.00e+01" or "+0x1a" or " 12,000 " or " 12.000 " to be integers?
0
 

Expert Comment

by:jammann
ID: 2526361
To check if the input just has digits, no points, commas or "e+" thingies, you can use

bourne/korn shell:

if expr "$input" : '^[0-9][0-9]*$' > /dev/null ; then

I personally consider c-shell to be evil for scripting, so I won't post a solution for that. Besides, I wouldn't know the syntax by heart ;-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:SrinivasK
ID: 2533019
jammann,
ur solution fails for negative integers.
0
 
LVL 2

Accepted Solution

by:
dirc earned 50 total points
ID: 2533380
try this script:

#!/bin/sh

printf "%d" $1 > /dev/null 2>&1
if [ $? = 0 ]; then
    echo is integer
else
    echo is not integer
fi

This will work for decimal integers that are in the signed 32-bit range and are bereft of "special" notation such as commas, decimal points and sci/egr notation.

Examples:

thisscript -123
is integer

thisscript abc
is not integer

thisscript 3000000000
is not integer # ed. too big
0
 
LVL 84

Expert Comment

by:ozo
ID: 2534277
thisscript "'x'"
is integer

thisscript 0099
is not integer

thisscript '1 2'
is integer

thisscript '- 2'
is not integer

thisscript ''
is integer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Separate files macro - Errors on larger data set 18 77
Named range not carried over 10 71
sumNumber challenge 16 147
countEvens challenge 2 117
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question