Checking if string contains integer


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking if string contains integer
# 1  
Old 09-10-2006
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  
Old 09-10-2006
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  
Old 09-10-2006
you can try this?
Code:
if ! [ $var -ge 0 -o $var -lt 0 ]; then 
    echo "string"
....

# 4  
Old 09-10-2006
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  
Old 09-10-2006
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  
Old 09-10-2006
Thanks, that did it perfectly. I changed it so it would check the other way round using -ne instead of -eq.
# 7  
Old 09-11-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting string to integer

I have a function that is supposed to check for user processes and wait for 0 count before exiting the function. I am sure I have more than one issue in my code, but the stumbling block right now is that I am trying to convert the value of my variable from a string to integer. process_count... (10 Replies)
Discussion started by: MIA651
10 Replies

2. Shell Programming and Scripting

String to integer

I am on HP-UX using ksh in the script. MaxSal=`sqlplus -silent /nolog <<EOF connect / as sysdba whenever sqlerror exit sql.sqlcode set pagesize 0 feedback off verify off heading off echo off select max(sal) from emp1; select max(sal) from emp2; select max(sal) from emp3; exit; EOF`... (3 Replies)
Discussion started by: bang_dba
3 Replies

3. UNIX for Dummies Questions & Answers

[Solved] How to extract integer out of a string?

Input: XXX:1,XXX:3,XXX:3 Output: A=1 B=3 C=3 Below code is what i do currently. However it doesn't work out for all cases. As some times the last digit extracted is giving me error. It cant be used for arithmetic computation. Any better methods out there? If possible, can you... (9 Replies)
Discussion started by: bananamen
9 Replies

4. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

5. UNIX for Dummies Questions & Answers

integer to string

Hi all, is there an easy way to convert integer to string in bash? I have numbers like 1, 2, ..., 112, ... and I would like to get 001 002 003 004 ... Thank you, Sarah (4 Replies)
Discussion started by: f_o_555
4 Replies

6. Shell Programming and Scripting

Comparing string and integer in IF

hi, I need to create an IF condition. I read a line from a file and get the 5 word using space as a delimited. This word can have only two values either '*' or '1-5' I need to write an IF condition for two cases. I can either compare it to * or 1-5(or even 1 by cutting and getting only the... (3 Replies)
Discussion started by: kaushys
3 Replies

7. Shell Programming and Scripting

Converting integer to String

Hi everyone, I would like to know how to convert an integer to a string. for instance if i=1 i would like to creat a variable called constant1. i want to do this in a for loop so for each value of i, i create a new variable such as constant2, constant3,... and so on. for i in 1 2 3 do ... (1 Reply)
Discussion started by: ROOZ
1 Replies

8. UNIX for Dummies Questions & Answers

conersting string to integer

Hi i am writing a shell script; I need to convert a string to integer so that i can use it in the 'awk' command to choose a field. can you please help me. thank you. (2 Replies)
Discussion started by: abb058
2 Replies

9. Programming

Integer to String

Which function should I use to convert an Integer to a String or Char format ? Thanx (2 Replies)
Discussion started by: psilva
2 Replies

10. UNIX for Dummies Questions & Answers

convert from an integer to a string

i want to convert from an integer to a string..in unix...i am writing a C program with embedded SQL... I remeber using itoa...but for some reason it doesnt work......i cant find it in the manual..... Maybe that is the wrong command..... but i have checked Dev Studio.....and it doest exist in the... (6 Replies)
Discussion started by: mojomonkeyhelper
6 Replies
Login or Register to Ask a Question