Unix Shell Script output..help needed

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 30
Reputation: crestaldin is an unknown quantity at this point 
Solved Threads: 0
crestaldin crestaldin is offline Offline
Light Poster

Unix Shell Script output..help needed

 
0
  #1
Dec 3rd, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Unix Shell Script output..help needed

 
0
  #2
Dec 4th, 2006
this should be in the shell scripting forum:

http://www.daniweb.com/techtalkforums/forum113.html
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 21
Reputation: risby is an unknown quantity at this point 
Solved Threads: 0
risby's Avatar
risby risby is offline Offline
Newbie Poster

Re: Unix Shell Script output..help needed

 
0
  #3
Dec 4th, 2006
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.
Good judgment comes from experience, and a lot of that comes from bad judgment.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 30
Reputation: crestaldin is an unknown quantity at this point 
Solved Threads: 0
crestaldin crestaldin is offline Offline
Light Poster

Re: Unix Shell Script output..help needed

 
0
  #4
Dec 4th, 2006
Thanks Risby but do you know why I keep getting :

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


for each filename (argument)
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Unix Shell Script output..help needed

 
0
  #5
Dec 5th, 2006
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC