944,193 Members | Top Members by Rank

Ad:
Dec 3rd, 2006
0

Unix Shell Script output..help needed

Expand Post »
Hello Everyone,
I need help on a part of the script below.The purpose is to accept some filenames as command line arguments and count the number of characters,words,lines in each file, and the total of each of these 3 attributes.

My output should look like this...

file 1 stats:
Number of characters: 100
Number of words : 50
Number of lines : 12

file 2 stats:
Number of characters: 200
Number of words : 65
Number of lines : 22



However my output presently looks like this:

$ ./test3 sush.pl sample
expr: syntax error
expr: syntax error
expr: syntax error
file 1 stats :
Number of characters : 75 sush.pl
Number of words : 12 sush.pl
Number of lines : 4 sush.pl

expr: syntax error
expr: syntax error
expr: syntax error
file 2 stats :
Number of characters : 102 sample
Number of words : 23 sample
Number of lines : 12 sample



I need help with taking out the expr: syntax error and the filename..like sush.pl,sample above that comes after each output

My code is as shown below

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2.  
  3. index=0
  4. sumChar=0
  5. sumWords=0
  6. sumLines=0
  7.  
  8.  
  9. for file in $*; do
  10. if [ -f "$file" ]; then
  11. {
  12. index=`expr $index + 1`
  13. char=`wc -c $file`
  14. words=`wc -w $file`
  15. lines=`wc -l $file`
  16.  
  17. sumChar=`expr $sumChar + $char`
  18. sumWords=`expr $sumWords + $words`
  19. sumLines=`expr $sumLines + $lines`
  20.  
  21. echo "file $index stats :"
  22.  
  23. echo "Number of characters : $char"
  24. echo "Number of words : $words"
  25. echo "Number of lines : $lines"
  26. echo " "
  27. }
  28. else
  29. {
  30. echo "$file does not exist"
  31. }
  32. fi
  33.  
  34. done
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
crestaldin is offline Offline
30 posts
since Mar 2005
Dec 4th, 2006
0

Re: Unix Shell Script output..help needed

this should be in the shell scripting forum:

http://www.daniweb.com/techtalkforums/forum113.html
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Dec 4th, 2006
0

Re: Unix Shell Script output..help needed

The wc program is outputting the filename after the count which interferes with the arithmetic.

Shell Scripting Syntax (Toggle Plain Text)
  1. char=`wc -c $file | cut -d' ' -f1`
  2. words=`wc -w $file | cut -d' ' -f1`
  3. lines=`wc -l $file | cut -d' ' -f1`

Just store the count in the variable by cutting out everything but the first field.
Reputation Points: 18
Solved Threads: 0
Newbie Poster
risby is offline Offline
21 posts
since Sep 2006
Dec 4th, 2006
0

Re: Unix Shell Script output..help needed

Thanks Risby but do you know why I keep getting :

expr: syntax error
expr: syntax error
expr: syntax error


for each filename (argument)
Reputation Points: 10
Solved Threads: 0
Light Poster
crestaldin is offline Offline
30 posts
since Mar 2005
Dec 5th, 2006
0

Re: Unix Shell Script output..help needed

Have you tried using his suggestion yet? If you had, you would see that the expression syntax error should now be gone. The problem was that the variables char, words, and lines contained something like "86 filename" rather than just "86". With the above change they will contain just "86". So, the expr syntax error is corrected.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: comparing dates using shell scripts
Next Thread in Shell Scripting Forum Timeline: Controlling the keyboard in linux?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC