Solved

number validation in shell scripts

Posted on 2000-02-15
6
348 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
ClickHouse in a General Analytical Workload

We have mentioned ClickHouse in some recent posts, where it showed excellent results.

In this article on Experts Exchange, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.

 
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

MongoDB Through a MySQL Lens

This article looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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 …
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

631 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