Fedora Linux Support Community & Resources Center
  #1  
Old 28th November 2004, 02:58 AM
rikuume's Avatar
rikuume Offline
Registered User
 
Join Date: Nov 2004
Location: California
Age: 31
Posts: 26
shell script IF statement

was wondering if someone could help me out real quick
I am trying to figure out why expansion isnt working in the way that I am trying to use it. I am running bash under FC2.

I am trying to make this work
if [ "$word" = *[aA]* ]
then
count=$(($count+1))
fi

I have also tried
if [ "$word" = "*[aA]*" ]
then
count=$(($count+1))
fi

If I use a complete value with no expansion such as [ "$word" = "hello" ] then it will work, but the expansion I try will not. Im pretty new at this. Any help or kick in the right direction would be great.

Thanks for the time and help!
Reply With Quote
  #2  
Old 28th November 2004, 03:21 AM
macemoneta's Avatar
macemoneta Offline
Registered User
 
Join Date: May 2004
Location: NJ
Posts: 913
I think this would be useful (see the section 7, Tests). For your particular application:

if echo "$word" | grep -q "[aA]"
Reply With Quote
  #3  
Old 28th November 2004, 03:35 AM
crackers's Avatar
crackers Offline
Retired Community Manager
 
Join Date: Feb 2004
Location: Seattle, WA, USA
Age: 56
Posts: 3,423
test can't do regular expressions, which is why the first two failed. macemoneta has the gist correct, but that particular expression does not work in bash. Here's a way that it does work:
Code:
echo $WORD | grep -q [aA]
if [ $? -eq 0 ] ;then
    echo success
fi
This checks the results of the last command run ($?), which in this case is grep. Note how the selection is denoted (both in macemoneta's post and here) - the brackets denote "match one or more of what's in here".
__________________
Linux User #28251 (April '93)
Professional Java Geek :cool:
Reply With Quote
  #4  
Old 28th November 2004, 03:56 AM
macemoneta's Avatar
macemoneta Offline
Registered User
 
Join Date: May 2004
Location: NJ
Posts: 913
Crackers: I verified proper execution of the construct in bash before posting. All you need is:

Code:
if echo "$word" | grep -q "[aA]"
then 
   echo "yes"
else 
   echo "no"
fi
The Advanced Bash Scripting Guide is really very useful. ;-)
Reply With Quote
  #5  
Old 28th November 2004, 05:11 AM
crackers's Avatar
crackers Offline
Retired Community Manager
 
Join Date: Feb 2004
Location: Seattle, WA, USA
Age: 56
Posts: 3,423
Huh! Guess I need to get more up2date (yuk, yuk, yuk) on my bash scripting.

If you want to get even sillier, you can write the above in one line:
Code:
echo "fubar" | grep -q [aA] && echo "yes" || echo "no"
However, I find both your construct as well as my one-liner to be rather cryptic and not very explicit. Too many years deciphering other folks' "clever" programming, I guess...
__________________
Linux User #28251 (April '93)
Professional Java Geek :cool:
Reply With Quote
  #6  
Old 28th November 2004, 05:23 AM
rikuume's Avatar
rikuume Offline
Registered User
 
Join Date: Nov 2004
Location: California
Age: 31
Posts: 26
Thanks for the help, both of you. I got it working now. Did not know I couldnt use expansions like that with the test.

Thanks again
__________________
"Insanity is doing the same thing over and over again
and expecting a different result."
Reply With Quote
  #7  
Old 28th November 2004, 05:27 AM
rikuume's Avatar
rikuume Offline
Registered User
 
Join Date: Nov 2004
Location: California
Age: 31
Posts: 26
Lets go a little farther with this, whats a good way to return how many times each one of those letters appears in the $word?

For example if there was 3 "a"s in it, how could I get it to see this and add one for each "a" in the variable?
__________________
"Insanity is doing the same thing over and over again
and expecting a different result."
Reply With Quote
  #8  
Old 28th November 2004, 05:32 PM
crackers's Avatar
crackers Offline
Retired Community Manager
 
Join Date: Feb 2004
Location: Seattle, WA, USA
Age: 56
Posts: 3,423
I think you've gone beyond the realm of shell scripting and into "real" programming. If you want to go really "old-school" you'll probably need to learn awk and/or sed. A little further up the programming chain, use Perl/Python/etc.
__________________
Linux User #28251 (April '93)
Professional Java Geek :cool:
Reply With Quote
  #9  
Old 28th November 2004, 06:49 PM
macemoneta's Avatar
macemoneta Offline
Registered User
 
Join Date: May 2004
Location: NJ
Posts: 913
Counting a character

You could accomplish the task for a specific character like this:

Code:
# The string to count letter "a" in
word="abacAbbaA"

# Get the count of "a" or "A"
# - Translate to lowercase
# - Remove uninteresting characters
# - Compress out space
# - Count the remaining characters
count=`echo "$word" | tr "[A-Z]" "[a-z]" | tr -d "[b-z]" | sed -e 's/ //g' | wc -c`

# Subtract one for the trailing newline
count=$(($count-1))

# Display the result
echo $count
If you'd like a count of the individual characters in a string, you can do it with something like this:

Code:
# The string to get unique character counts from
word="abacAbbaA"

# Get the counts
# - Translate upper to lowercase so "A" and "a" are counted together
# - Split each character to a separate line
# - Sort them
# - Get the unique counts
# - Remove the bogus count for the trailing newline
counts=`echo "$word" | tr "[A-Z]" "[a-z]" | sed -e 's/./&\n/g' | sort | uniq -c | grep -v -e '^ *1 *$'`

# Display the results
echo $counts
Reply With Quote
  #10  
Old 29th November 2004, 04:26 AM
rikuume's Avatar
rikuume Offline
Registered User
 
Join Date: Nov 2004
Location: California
Age: 31
Posts: 26
Quote:
Originally Posted by crackers
I think you've gone beyond the realm of shell scripting and into "real" programming. If you want to go really "old-school" you'll probably need to learn awk and/or sed. A little further up the programming chain, use Perl/Python/etc.
Yeah I thought it might be. I am just trying to think of projects that will help me learn scriptting well. Any suggestions on a good project or two that will incorperate a lot of commands and use of shell scripting?

Thanks macemoneta, I will work with that and see if it works for me. Thanks for the help guys!
__________________
"Insanity is doing the same thing over and over again
and expecting a different result."
Reply With Quote
Reply

Tags
script, shell, statement

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
writting a script with a IF statement (Programming) akounga Programming & Packaging 4 15th June 2009 10:48 AM
call remote shell script within an expect script PhillyFloyd Programming & Packaging 2 16th October 2007 10:29 PM
Shell Script IF statement Elliot Using Fedora 5 30th July 2007 04:50 AM
starting a shell script inside a php script gw348 Using Fedora 6 23rd April 2007 09:30 PM
need to run script Bourne-shell script armen Using Fedora 4 4th April 2005 01:16 PM


Current GMT-time: 18:07 (Wednesday, 22-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat