making sure my string is [a-z] only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting making sure my string is [a-z] only
# 1  
Old 04-24-2009
making sure my string is [a-z] only

Hi there

I am trying to figure out how I can validate my string to ensure that it is consists of only lower case alpha chrataters [a-z]

but for some reason whatever I do, it seems to come back with 'true'

I have tried



Code:
		if [[ $VAR="[^a-z+]" ]] ; then
		    echo "yes its fine"
		else
		     echo "nope"
		fi

and ive also tried (amongst other variations)


Code:
	  case $VAR in
		[a-z]) echo "string is ok" ;;
		*) 	  echo "string is not ok" ;;
		  esac

both of these return true whatever i do


Is there something im doing here thats wrong

Cheers
# 2  
Old 04-24-2009
Try...
Code:
$ for VAR in Aa A a
> do
>  if [[ $VAR = $(echo $VAR|tr -dc '[a-z]') ]] && [[ -n $VAR ]]
>  then
>      echo $VAR is ok
>  else
>      echo $VAR is not ok
>  fi
> done
Aa is not ok
A is not ok
a is ok
$

# 3  
Old 04-24-2009
Code:
if [[ $VAR = *([a-z]) ]]; then
  echo "yes its fine"
else
  echo "nope"
fi

*([a-z]) allows empty string. If an empty string is not fine, then use +([a-z]).
# 4  
Old 04-24-2009
With case:

Code:
case $var in 
  "" | *[!a-z]* ) echo invalid ;; 
              * ) echo ok ;;
esac

# 5  
Old 04-24-2009
Quote:
Originally Posted by colemar
Code:
if [[ $VAR = *([a-z]) ]]; then
  echo "yes its fine"
else
  echo "nope"
fi

*([a-z]) allows empty string. If an empty string is not fine, then use +([a-z]).
The original poster wants to ensure that the string contains only lower case alpha chrataters ...
# 6  
Old 04-24-2009
Quote:
Originally Posted by radoulov
With case:

Code:
case $var in 
  "" | *[!a-z]* ) echo invalid ;; 
              * ) echo ok ;;
esac


Thanks for your help (all of you)

radoulov ... It still seems to have problems with capital letters
for example, look at this output from your case statement above

Code:
peter
ok

peter1
invalid

Peter
ok

PETER
ok


The capitals seem to be a problem ... thanks for your help though
# 7  
Old 04-24-2009
It's because of your locale, try setting LANG=C for the duration of the script:

Code:
% cat s
case $var in
  "" | *[!a-z]* ) echo invalid ;;
              * ) echo ok ;;
esac
% var=A
% LANG=it.UTF-8 . s
ok
% LANG=C . s
invalid


Last edited by radoulov; 04-24-2009 at 06:51 AM.. Reason: LANG is sufficient ...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help me making this script

This script is executed whenever a new vehicle is added to the cycle-motor park of campus. The script asks for the following information about the car and adds a new line to the vehicle file.txt: name (name of an animal, unique identifier), color, mark, model, type (e.g., electrical, manual),... (2 Replies)
Discussion started by: andre2222
2 Replies

2. Shell Programming and Scripting

I could use some help with making a script

I run a small instrument lab. We track our user's time on the instruments with a very manual process of 'last wtmp.1' then cut/paste data into spreadsheets. My boss makes the initial spreadsheets then I convert and format them for uploading into our billing software (COReS). Cores is looking for a... (8 Replies)
Discussion started by: jpontius
8 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

4. UNIX for Advanced & Expert Users

Regarding help for making own OS

Dear Fellow, I want to make my own OS, Kindly suggest from where i should start. please help me out. (2 Replies)
Discussion started by: zaigham_tt
2 Replies

5. Shell Programming and Scripting

Making array of string with bash

in.txt libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good gstreamer-0_10-plugins-base Output should be: libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good gstreamer0_10-plugins-base Then: #!/bin/sh v=(libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good... (5 Replies)
Discussion started by: cola
5 Replies

6. Shell Programming and Scripting

Making Variables

Dear Friends, Here I need your help once again. I have a flat file with pipe de-limited format e.g. 12345|1234567890|0|0|0| (Total 5 values) I want to take all non 0 ("Zero") values in variables named as anu1, anu2, anu3, anu4 and anu5. Is it possible? Please guide me. Thank you in... (3 Replies)
Discussion started by: anushree.a
3 Replies

7. UNIX and Linux Applications

help making a library.

I understand how to use vi and emacs but I have a project which entails building a library application like a phone directory or listing of dvd's. I am lost on where to start. any help would be appreciated. (1 Reply)
Discussion started by: gustave
1 Replies

8. Shell Programming and Scripting

making use of a Semaphore

can any one tell me how to run a semaphore. i understand roughly how they work code wise, but i'm not sure how to make use of them. i have two client programs in csh that perform tasks on a server file (flat file) how the i get the semaphore to lock the file when one has accessed it and how to use... (5 Replies)
Discussion started by: FDavid
5 Replies

9. UNIX for Dummies Questions & Answers

making a variable as string

I am evaluating a variable from a database and storing it as inside. The value of the variable is alpha numeric.How can i make this a string type.Any functions for the same. (1 Reply)
Discussion started by: dr46014
1 Replies
Login or Register to Ask a Question