The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Converting integer to String ROOZ Shell Programming and Scripting 1 06-05-2008 01:38 PM
conersting string to integer abb058 UNIX for Dummies Questions & Answers 2 08-23-2006 07:52 AM
C function to test string or integer qqq High Level Programming 3 03-10-2005 01:55 AM
Integer to String psilva High Level Programming 2 08-17-2001 12:14 PM
convert from an integer to a string mojomonkeyhelper UNIX for Dummies Questions & Answers 6 03-29-2001 04:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-10-2006
haz haz is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 3
Checking if string contains integer

G'day guys, first post so be gentle.

I need help with some code to work out if a variable (string) contains any integers. The valid variable (string) must contain only letters.

Also need to be able to work out if a variable contains only integers.

Any help greatly appreciated.
  #2 (permalink)  
Old 09-10-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Welcome to the forums. We have a search feature which you can use to see if similiar problems have encountered before.

See - ksh : find value type
  #3 (permalink)  
Old 09-10-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,533
you can try this?
Code:
if ! [ $var -ge 0 -o $var -lt 0 ]; then 
    echo "string"
....
  #4 (permalink)  
Old 09-10-2006
haz haz is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 3
Sorry, should have specified it is a Bourne shell script that I am writing.

This is what I've tried writing, where am i going wrong?

Code:
namenum=$1

if [ "$namenum" -eq *[a-zA-Z]* ] ; then
      echo "$namenum is all alphabetic"
else
      echo "not all alphabetic"
fi
  #5 (permalink)  
Old 09-10-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 579
Try:
Code:
#! /bin/sh

namenum=$1

echo $namenum | grep [0-9] > /dev/null
if [ $? -eq 0 ] ; then
        echo "non-alpha"
else
        echo "alpha"
fi
  #6 (permalink)  
Old 09-10-2006
haz haz is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 3
Thanks, that did it perfectly. I changed it so it would check the other way round using -ne instead of -eq.
  #7 (permalink)  
Old 09-11-2006
Andrek Andrek is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 122
try "tr"
Do a man on tr

basiclly

# echo "AbCd67Fg8" | tr -d "[:alpha:]"
# 678

So test for null variables

ie

#!/bin/sh
var=AbCd67g8
if [ -z `echo $var | tr -d "[:alpha:]"` ]
then
echo "ONLY Alpha chars here!" # You had only A-z
else
echo "Sorry you have other stuff" # You have soming else besides A-z?
fi
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0