Solved

number validation in shell scripts

Posted on 2000-02-15
6
345 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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