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
problem with dd command or maybe AFS problem Anta Shell Programming and Scripting 0 08-25-2006 10:10 AM
annoying vi yank word + delete all question umen UNIX for Dummies Questions & Answers 4 03-20-2006 08:46 AM
SSH Problem auth problem budrito UNIX for Advanced & Expert Users 1 03-17-2004 10:12 AM
a very annoying problem Stormpie UNIX for Dummies Questions & Answers 2 04-24-2002 12:29 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-12-2008
shadow0001 shadow0001 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 10
Very ANNOYING Problem - Please Help

Hey Guys

I have an extremely annoying problem with regular expressions! At this point i believe the command 'read' is causing the problem due to the carriage return it places once its done.

I have an continuous loop until the input is correct: (After initial read statement)

Code:
while [ 0 ]
        do
                if [[ "$word" == [a-z]+ ]]
                then
                        word=$(echo $word | tr "[A-Z]" "[a-z]")
                        encrypted=$(echo $word | tr "[a-z]" "-")
                        display $encrypted $word
                        break
                else
                        echo "Please enter a word consisting of only the letters a-z."
                        read word
                fi
        done
Whats highlighted in bold is the problem. Every time it reaches it, nothing is allowed ! Not even if the input is completely valid as in:

abcd

What i want it to do is accept ONLY a-z characters and nothing else - no spaces either. In theory it should work however in practice i really do believe the 'read' command is giving me the trouble. Maybe a chop-esque function is required such as the one in perl.

I hope you guys can help! Thank You. -shell is BASH
  #2 (permalink)  
Old 03-12-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Well I'm not a super bash expert, but that does what I guessed it might...
Code:
bash-2.03$ x="abc"
bash-2.03$ [[ $x == [a-z]+ ]] && echo it matches
bash-2.03$ x="d+"
bash-2.03$ [[ $x == [a-z]+ ]] && echo it matches
it matches
bash-2.03$
  #3 (permalink)  
Old 03-12-2008
shadow0001 shadow0001 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 10
Yeah, it seems so pointless ... Either the syntax is wrong or the guys behind the development of BASH got this one slightly wrong ...

I mean what i can do is individually check each character but its excessive code - 5 lines compared with 1. So if the experts can give me some advice, it would be much appreciated.

Thanks.
  #4 (permalink)  
Old 03-12-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
Hi.

The alternate syntax seems to work:
Code:
#!/bin/bash3 -

# @(#) s1       Demonstrate extended glob matching.
# See man bash "Pattern matching" and extglob shopt.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1)

x="abc"
if [[ $x == [a-z]+ ]]
then
  echo " Matched trailing +."
fi

shopt -s extglob
if [[ $x == +([a-z]) ]]
then
  echo " Matched leading +."
fi

exit 0
Producing:
Code:
% ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 3.00.16(1)-release
 Matched leading +.
Best wishes ... cheers, drl
  #5 (permalink)  
Old 03-12-2008
shadow0001 shadow0001 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 10
Hey drl,

That does not seem to work on my end, possibly because your shell is bash3 but im not too sure of the difference between it and bash. I tried using the reverse syntax but no luck there, i get a few errors in doing so.

It does look like I'm going to have to use some longer code - just seems so silly that this little bug (if thats what it actually is) is causing me grief lol. Thanks for your help though.
  #6 (permalink)  
Old 03-12-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
Hi.

What shell are you using? ... cheers, drl
Closed Thread

Bookmarks

Tags
linux, regex, regular expressions

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 05:24 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