Solved

Checking if a string contains a letter in ksh

Posted on 2004-04-26
8
3,543 Views
Last Modified: 2007-12-19
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

0
Comment
Question by:r47463
  • 2
  • 2
  • 2
  • +1
8 Comments
 

Author Comment

by:r47463
ID: 10923312
PS rather than just letters, I would like any non-numeric characters to be invalid.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 10924787
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
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 10924822
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
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 10925105
Hi r47463,
use isnumeric function to determine whether its a number or not

Cheers!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 10925274
[!0-9] represents all non numeric characters .. 0-9 is the range for numeric characters and ! is negation
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 10926896
sorry wrongly interpreted it for VB.

Regards,
Bhagyesh
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 40 total points
ID: 10927170
>At the moment I don't know how to check when a string has both a number and a letter; it just crashes out.

execute this

echo $u | grep "[^0-9]" >dev/null
then check for value of $? ... If 1 then it was composed of characters from 0-9

if [ $? -eq 1 ]
then
          echo valid
else
          echo invalid
fi
0
 

Author Comment

by:r47463
ID: 10932270
Superb thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now