LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2006, 08:55 AM   #1
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
bash: howto check string is integer?


bash:
howto check string is integer?

Last edited by ygloo; 01-15-2007 at 05:33 PM.
 
Old 10-14-2006, 10:59 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Use expr command to multiply it by 1. If its not a number it will tell you so. If it is a number it will just give you the original number.

Script to do same follows. Just vi a file called "testint.sh" and insert this code. Run it from command line as:

testint.sh INPUT

Where INPUT is the item you are testing. (Make sure to add execute bit with chmod command or run it as "sh testint.sh INPUT" instead.)


Code:
#!/bin/bash
#
# First multiply the input argument times 1.  If input is a number this will
# result in original number.  Otherwise it will error out.  Send output and
# error to /dev/null so they don't show up on screen.
#
# Put that inside a conditional.  If return fom the expr command is false it
# is not an integer.  If it is true then it is an integer.

if expr $1 \* 1 >/dev/null 2>&1
then echo it is an integer
else echo it is not an integer
fi
 
Old 10-14-2006, 11:05 AM   #3
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
There might be better ways, but this one is the one I can think of right now:

Code:
echo $MY_VAR  | grep "^[[:digit:]][[:digit:]]*$" > /dev/null
# NO CODE HERE OR YOU CAN OVERWRITE $? !!!!
if [ "$?" -eq 0 ]; then
	echo "Its a number"
else
	echo "its a string"
fi
Hope this is useful.
Cheers!
 
Old 10-14-2006, 11:08 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Code:
###
### Sample script
###

# Set variable l_string to a valid integer
l_string="10" 

# use sed to remove a leading + or - symbol and replace
# one or more numbers from 0-9 from the string and count
# the remaining characters ( should leave one character )

l_is_int=`echo $l_string|sed 's/^[+-]//g'| \
sed 's/[0-9][0-9]*//g'|wc -c`

if [ $l_is_int == 1 ]
then
   echo "l_string is a valid integer value"
else
   echo "l_string is not a valid integer value"
fi
 
Old 10-14-2006, 11:09 AM   #5
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30
Code:
read 

expr $REPLY + 0 > /dev/null || exit 1
# this works
Code:
case "$REPLY" in
*[0-9]*  ) true   # woks when string is - 22424 or sdfasdjfasdj, when mixed - dsf435 or 345dsf - not workin
esac

Last edited by ygloo; 10-14-2006 at 12:08 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c convert integer to string? kpachopoulos Programming 5 08-23-2006 11:57 AM
C++ string to integer conversion fatman Programming 6 06-18-2006 08:17 AM
C how to convert an integer to string? totti10 Programming 10 11-10-2005 05:49 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM
How do you convert a string to an integer? wbdune Linux - Newbie 11 11-01-2003 02:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration