Skip to main content
added 4 characters in body
Source Link
Stranger
  • 337
  • 1
  • 2
  • 7

I have a source code to find a number of words and characters in a file:

#!/bin/bash
w=0
cc=0
for i in `cat $1`
do
j=$i
echo $j
w=$(($w+1))
c=`expr $j:'.*'`
cc=$(($cc+$c))
done
echo "no of characters"  $cc
echo "no of words" $w

But when I run it in the terminal the following error message is displayed^ ./countWordChar 1.c hello ./countWordChar: line 10: 0+hello0+hello:.*:.: syntax error in expression (error token is ":." syntax error in expression (error token is ":.*") no of characters 0 no of words 1

The 10th line in the code is cc=$(($cc+$c)). Apparently the c variable's value is not a number of characters of a word but the word itself.

And my 1.c file content is like that:

hello world
hello

What's wrong in the code?

PS. I know that there are builtin commands to count characters in a file but I must use the previous code according to my task.

I have a source code to find a number of words and characters in a file:

#!/bin/bash
w=0
cc=0
for i in `cat $1`
do
j=$i
echo $j
w=$(($w+1))
c=`expr $j:'.*'`
cc=$(($cc+$c))
done
echo "no of characters"  $cc
echo "no of words" $w

But when I run it in the terminal the following error message is displayed^ ./countWordChar 1.c hello ./countWordChar: line 10: 0+hello:.: syntax error in expression (error token is ":.") no of characters 0 no of words 1

The 10th line in the code is cc=$(($cc+$c)). Apparently the c variable's value is not a number of characters of a word but the word itself.

And my 1.c file content is like that:

hello world
hello

What's wrong in the code?

PS. I know that there are builtin commands to count characters in a file but I must use the previous code according to my task.

I have a source code to find a number of words and characters in a file:

#!/bin/bash
w=0
cc=0
for i in `cat $1`
do
j=$i
echo $j
w=$(($w+1))
c=`expr $j:'.*'`
cc=$(($cc+$c))
done
echo "no of characters"  $cc
echo "no of words" $w

But when I run it in the terminal the following error message is displayed^ ./countWordChar 1.c hello ./countWordChar: line 10: 0+hello:.*: syntax error in expression (error token is ":.*") no of characters 0 no of words 1

The 10th line in the code is cc=$(($cc+$c)). Apparently the c variable's value is not a number of characters of a word but the word itself.

And my 1.c file content is like that:

hello world
hello

What's wrong in the code?

PS. I know that there are builtin commands to count characters in a file but I must use the previous code according to my task.

Source Link
Stranger
  • 337
  • 1
  • 2
  • 7

Error while counting characters in a file

I have a source code to find a number of words and characters in a file:

#!/bin/bash
w=0
cc=0
for i in `cat $1`
do
j=$i
echo $j
w=$(($w+1))
c=`expr $j:'.*'`
cc=$(($cc+$c))
done
echo "no of characters"  $cc
echo "no of words" $w

But when I run it in the terminal the following error message is displayed^ ./countWordChar 1.c hello ./countWordChar: line 10: 0+hello:.: syntax error in expression (error token is ":.") no of characters 0 no of words 1

The 10th line in the code is cc=$(($cc+$c)). Apparently the c variable's value is not a number of characters of a word but the word itself.

And my 1.c file content is like that:

hello world
hello

What's wrong in the code?

PS. I know that there are builtin commands to count characters in a file but I must use the previous code according to my task.