0

This script is supposed to look at my parameters and tell me if the word begins with an uppercase, lowercase, or digit. Right now when i put in a # of words it tells me that they are all uppercase. Thank you.

#!/bin/bash

startwithdigit=0
startwithupper=0
startwithlower=0
startwithother=0

for digit in $@

do
case $@ in

    [[:lower:]]* ) startwithlower=$((startwithlower+1)) ;;

    [[:upper:]]* ) startwithupper=$((startwithupper+1)) ;;

    [[:digit:]]* ) startwithdigit=$((startwithdigit+1)) ;;

esac

done

echo $startwithlower words begin with a lowercase
echo $startwithupper words begin with a capital
echo $startwithdigit words begin with a digit

~ ~

2
  • 2
    Hint: you never use digit in the body of the for loop, and $@ has the same value in every iteration of the loop that is iterating over its value. Commented Oct 5, 2016 at 23:59
  • you can simplify the arithmetic: ((startwithlower++)) etc Commented Oct 6, 2016 at 1:32

1 Answer 1

1

Try this below:

I am using if statements instead of case, and i am counting by appending in file and later taking word count, instead of adding 1 each time to a counter.

# Sample data

"India China dubai germany 123 456 666" to the script. 

 It has 2 uppercase words, 2 lowercase words and 3 digit words.

# Sample output

bash$> ./upper_lower.sh "India China dubai germany 123 456 666"

2 words are lowercase

2 words are uppercase

3 words are digits

# The script.

bash$> cat upper_lower.sh
#!/bin/bash


# Main loop to process the words passed to script in ARGV array

for w in $@

do
        echo $w | while read word
        do

                # Matching lowercase only
                if [[ $word =~ ^[a-z] ]]
                then
                        echo $word >> aa
                fi

                # Matching UPPERCASE only
                if [[ $word =~ ^[A-Z] ]]
                then
                        echo $word >> bb
                fi

                # Matching digits only
                if [[ $word =~ ^[0-9] ]]
                then
                        echo $word >> cc
                fi

        done
done

l=`cat aa|wc -l`        # Taking lowercase count
u=`cat bb|wc -l`        # Taking uppercase count
d=`cat cc|wc -l`        # Taking digits count

echo; echo "$l words are lowercase"     # Added extra echo just to print new line.
echo; echo "$u words are uppercase"
echo; echo "$d words are digits"

rm aa bb cc             # removing temp files after processing
bash$>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.