Link to home
Start Free TrialLog in
Avatar of r47463
r47463

asked on

Checking if a string contains a letter in ksh

Hi, I'd like to check a string for the presence of letters.

E.g.

"123" valid
"abc" invalid
"123abc"  invalid

At the moment I don't know how to check when a string has both a number and a letter; it just crashes out.

My current code is as follows:

i=1
until (test $i -gt 4)
do
  echo "$arg1"|cut -f$i -d'.'|read octet
  if [[ "$octet" = +([a-z]) ]]
  then
    echo "Error - invalid IP address entered"
    IPvalid=0
    i=4
  else
    if [[ $octet -ge 0 && $octet -le 255 ]]
    then
      IPvalid=1
    else
      echo "Error - invalid IP address entered"
      IPvalid=0
      i=4
    fi
  fi
  i=`expr $i + 1`
done

Thanks

Avatar of r47463
r47463

ASKER

PS rather than just letters, I would like any non-numeric characters to be invalid.
Avatar of FishMonger
Would you be interested in using Perl instead of ksh?

#!/usr/bin/perl -w

use strict;
use Validate::Net;

my $good = '123.1.23.123';
my $bad1 = '123.432.21.12';
my $bad2 = 'abc.232.21.12';

foreach ( $good, $bad1, $bad2 ) {
   if ( Validate::Net->ip( $_ ) ) {
      print "'$_' is a valid ip\n\n";
   }
   else {
      print "'$_' is not a valid ip address because:\n";
      print Validate::Net->reason . "\n\n";
   }
}


-- outputs --
'123.1.23.123' is a valid ip

'123.432.21.12' is not a valid ip address because:
The maximum value for an ip element is 255

'abc.232.21.12' is not a valid ip address because:
Does not fit the basic dotted quad format for an ip
That code sample is from the CPAN documentation for the Validate::Net module.

http://search.cpan.org/~adamk/Validate-Net-0.5/lib/Validate/Net.pm
Hi r47463,
use isnumeric function to determine whether its a number or not

Cheers!
[!0-9] represents all non numeric characters .. 0-9 is the range for numeric characters and ! is negation
sorry wrongly interpreted it for VB.

Regards,
Bhagyesh
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of r47463

ASKER

Superb thanks!