Check if variable is a number


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Check if variable is a number
# 1  
Old 03-01-2005
Check if variable is a number

If I have a variable $X, how do I check it is a number?

Many thanks.
# 2  
Old 03-01-2005
A search of the forums would have given this - there is a multiple purpose variable validation script at the end of the thread.

A simple solution would be (for positive integers)
Code:
echo $X | egrep '^[0-9]+$' >/dev/null 2>&1
if [ "$?" -eq "0" ]; then
  echo "Postive integer here...."
else
  echo "Something else...."
fi

Cheers
ZB
# 3  
Old 03-01-2005
You can also use typeset -i to define your variable as an integer. You would also have to dump the error stream to /dev/null if you attempt to assign a string to the variable (this is easily done by simply by redirection 2> /dev/null)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

number of instance check

Hi, We are getting a curios result in one of AIX script. Its executed using !/bin/ksh . After following line we get result of 3 in in the variable instance_count. instance_count=`ps -ef | grep "script_check_instances.sh" | grep -v "grep" | wc -l` But once we do a "ps -aef | grep... (2 Replies)
Discussion started by: niba
2 Replies

2. Shell Programming and Scripting

Check params for number

I have 2 and three params, both I should make sure thay numbers at one single line insted of checking for each one . Example I wroote the following way.. checking for 2 and three seperately but I shud be able to do it at on statement echo $2 | egrep '^+$' >/dev/null 2>&1 if ; then echo... (2 Replies)
Discussion started by: raopatwari
2 Replies

3. Shell Programming and Scripting

Check if a variable ends in a particular number

Hi guys, I am working on a server where there are many users. The user names end in a 1 or a 2. I want to write a bash script that will say which users are in which group and was wondering if I could get some help. The only part I am unsure of is how to check if it ends in the number. Here's... (2 Replies)
Discussion started by: wua05
2 Replies

4. Shell Programming and Scripting

Check if the value is Number

Hi, I have a file with data as given $cat file1.txt 123 234 23e 234.456 234.876e 345.00 I am checking if the values are proper integers using the command. nawk -F'|' 'int($1)!=$1 {printf "Error in field 1|"$0"\n"}' file1.txt This is checking for only integers ( without... (10 Replies)
Discussion started by: ashwin3086
10 Replies

5. Shell Programming and Scripting

To check a word is number or not

Hi, I have one file like 00123. And this file name is generated as a sequence. So how can I confirm the generated file name is a number, not a special character or alphabets. Can anybody help me out. Thanks in advance. (3 Replies)
Discussion started by: Kattoor
3 Replies

6. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

7. Shell Programming and Scripting

number check in perl

Hi,, this is returning true in all cases..( other than 10 dig number also) what could be wrong?? (2 Replies)
Discussion started by: shellwell
2 Replies

8. Shell Programming and Scripting

How to check whether a string is number or not

Hi , I am facing a problem .. which looks simple... but took 2 days of mine.. even now it is not solved completely.. I have one variable..., want to know whether that variable contains number... canbe +ve or -ve ... Values +35 --- number -43 --- number 45A -- non number... (12 Replies)
Discussion started by: shihabvk
12 Replies

9. Shell Programming and Scripting

Check if a variable is a number - perl

Logic of code if ( $var is a number ) { Do something } else { Do something else } My question is: How do I check if a variable is a number. All the reg ex that I came up with to match this is failing. Please help. (3 Replies)
Discussion started by: garric
3 Replies

10. UNIX for Dummies Questions & Answers

check whether variable number or string

I am passing an argument to a file and i wanna check whether the argument is a number or string ? how can i do this? (4 Replies)
Discussion started by: rolex.mp
4 Replies
Login or Register to Ask a Question