PDA

View Full Version : shell script IF statement



rikuume
28th November 2004, 03:58 AM
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! :D

macemoneta
28th November 2004, 04:21 AM
I think this (http://www.tldp.org/LDP/abs/html/) would be useful (see the section 7, Tests). For your particular application:

if echo "$word" | grep -q "[aA]"

crackers
28th November 2004, 04:35 AM
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:


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".

macemoneta
28th November 2004, 04:56 AM
Crackers: I verified proper execution of the construct in bash before posting. All you need is:


if echo "$word" | grep -q "[aA]"
then
echo "yes"
else
echo "no"
fi

The Advanced Bash Scripting Guide is really very useful. ;-)

crackers
28th November 2004, 06:11 AM
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:


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... :D

rikuume
28th November 2004, 06:23 AM
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 :D

rikuume
28th November 2004, 06:27 AM
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?

crackers
28th November 2004, 06:32 PM
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.

macemoneta
28th November 2004, 07:49 PM
You could accomplish the task for a specific character like this:


# 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:


# 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

rikuume
29th November 2004, 05:26 AM
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! :D