• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me George!!!!!  RSS feed

 
sujatha mamidala
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could you please let me know what's wrong with this script.
#!/bin/sh
for FILENAME in *.jsp
do
echo "processing ${FILENAME}"
name=Zaizar
cnt='grep -c $name ${FILENAME}'
echo $cnt
done
Instead of giving me the count it's simply printing
'grep -c $name ${FILENAME}'
Please let me know what's wrong here.
Thanks A Lot,
Suja.
 
George Brown
Ranch Hand
Posts: 919
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're using ' when you should be using `
cnt='grep -c $name ${FILENAME}'
should be
cnt=`grep -c $name ${FILENAME}`
Where the two characters are on your keyboard depends on what type of keyboard (layout) you have. Mine's a UK PC keyboard: the ' character on mine is on the same key as the @ character (mid-right of the keyboard), and the ` character is on the same key as the � character (upper-left of the keyboard).
I hope that helps.
 
George Brown
Ranch Hand
Posts: 919
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, but I've got my nitpicking hat on. (well you did ask for my opinion...)
Also, if I had declared a variable called 'name' I would have declared it before the 'for FILENAME in *.jsp' line, otherwise it is being redeclared needlessly for every iteration of the loop.
And finally, stylistic things: when I write shell scripts I generally use capital letters for variables, that way it makes it easier to distinguish the shell variables from the commands at a glance. And try to be consistent with variables:
rather than declaring like this:
cnt=`grep -c $name ${FILENAME}`
why not be consistent: either do
cnt=`grep -c ${NAME} ${FILENAME}`
or
cnt=`grep -c $NAME $FILENAME`
Last but not least, an old chinese proverb states that all wisdom begins with naming things by their real names.
(so cnt or CNT becomes count or COUNT etc etc)
[This message has been edited by George Brown (edited March 16, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!