0

Why can't I create a sum of total words in this script? I get the result something like:

 120+130

but it isn't 250 (as I expected)! Is there any reason?

#!/bin/bash

while [ -z "$count" ] ;
do
    echo -e "request :: please enter file name "
    echo -e "\n\tfile one : \c"
    read count

    itself=counter.sh

    countWords=`wc -w $count |cut -d ' ' -f 1`
    countLines=`wc -l $count |cut -d ' ' -f 1`
    countWords_=`wc -w $itself |cut -d ' ' -f 1`

    echo "Number of lines: " $countLines
    echo "Number of words: " $countWords
    echo "Number of words -script: " $countWords_

    echo "Number of words -total " $countWords+$countWords_  

done

if [ ! -e $count ] ; then
    echo -e "error :: file one $count doesn't exist. can't proceed."
    read empty
    exit 1
fi
1
  • 2
    don't use <br>, use the "code sample (ctrl-K)" button. Commented Jan 6, 2011 at 5:19

2 Answers 2

6

echo "Number of words -total " $countWords+$countWords_

You want this:

echo "Number of words -total $((countWords + countWords_))"

Edit

Here are some optimizations to your script.

  1. The while loop seems pointless since count is going to get set for sure inside making this a 1-iteration while loop.
  2. Your if check on existence of the file should happen before you ever use that file.
  3. You don't need to hardcode the name of your script to the variable itself, you can use $0 for that
  4. Since you are using bash I took the liberty to remove the need for cut by using process substitution.

Here is the revised script:

#!/bin/bash

echo -e "request :: please enter file name "
echo -e "\n\tfile one : \c"
read count

if [ ! -e "$count" ] ; then
    echo "error :: file one $count doesn't exist. can't proceed."
    exit 1
fi

itself="$0"

read countWords _ < <(wc -w $count)
read countLines _ < <(wc -l $count)
read countWords_ _ < <(wc -w $itself)

echo "Number of lines: '$countLines'"
echo "Number of words: '$countWords'"
echo "Number of words -script: '$countWords_'"

echo "Number of words -total $((countWords + countWords_))"
Sign up to request clarification or add additional context in comments.

1 Comment

countWords=$(wc -w < "$count") will get the count without the file name. Or you could use one call to wc instead of two: read countLines countWords <<< $(wc -lw < "$count"). $count could be null if the user presses enter. This will cause the while loop to continue looping until the user inputs something.
1

One way to go about this is:

echo `expr $countWords + $countWords_`

6 Comments

expr has been deprecated, use $(( )) instead
@SiegeX, $(( )) is bash-syntax, don't break posix bourne shell.
$(( ... )) is POSIX shell; it is also Korn shell (and Bash); it is not Bourne shell.
@Jonathan thanks for doing my dirty work. This convo seems eerily familiar =)
@J-16 even if $(( )) weren't POSIX syntax, the #!/bin/bash at the top of his script would make this a pretty moot point
|

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.