2

i try to get line count in the every log files as bellow code but bellow code gave to me error as No such file or directory in line_count=$(wc -l < "{$entry}") line i can't understand why that gave error because in the above line i print file name also

#!/bin/bash

FILE="/var/log"

declare -i line_count

for entry in $FILE/* ; do
   echo "{$entry}" # auth.log
    line_count=$(wc -l < "{$entry}") # No such file or directory

    echo "{$line_count}"
done
DD Dev
  • 859
  • 1
  • 6
  • 11

2 Answers2

1

You make a mistake using shell variables.

Try replacing {$variable} with ${variable}

#!/bin/bash

FILE="/var/log"

declare -i line_count

for entry in $FILE/* ; do
   echo "${entry}" # auth.log
   line_count=$(wc -l < "${entry}") # change to this

   echo "${line_count}"
done

more about shell variables and braces

Community
  • 1
  • 1
luoluo
  • 5,353
  • 3
  • 30
  • 41
-1

It's the curly braces around $entry. Remove them:

line_count=$(wc -l < "$entry")
Georg P.
  • 2,785
  • 2
  • 27
  • 53
  • Your suggestion would break the script funtionality if there were files with the spaces for some reason in that dir – Zloj Aug 26 '15 at 06:57
  • The curly braces (if put correctly as in `${entry}`) are correct, a good habit to get into, and actually necessary if you want to get something like `wc -l "${foo}_${bar}"`. Not my downvote though. @JanLutenko: Actually, no -- it's the quotation marks that ensure correct behaviour here, not the braces. – DevSolar Aug 26 '15 at 06:59
  • @DevSolar oh, yes you're right. I have a habit of always using "${}" and mixed those two just because I'm sure I don't have to worry about variable expansion any more. – Zloj Aug 26 '15 at 07:06