Solved

number validation in shell scripts

Posted on 2000-02-15
6
346 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
set up trigger (or something) to rename files (starting with ATL) in a linux directory 6 55
Generic progress indicator 6 117
EvenOdd challenge 10 128
FizzBuzz challenge 9 82
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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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