Solved

number validation in shell scripts

Posted on 2000-02-15
6
342 Views
Last Modified: 2013-12-26
How to validate whether the input from keyboard is an integer or not?
0
Comment
Question by:prasen120998
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now