Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I realized a bash script that works on an existing text file located into the same working directory of the script. The script take name of the file to compute through a function parameter.

The issue is that when I run the script it does not locate the file. I've already checked positional parameter of the function when called and the name of the file is correct. But a check on that filename "[ -e filename ]" or a command launched on it (ex. cat filename) returns an error. Filename not found.

I paste my script..



function count(){
  local valore=0
  local wordcmp

  if [ -n "$query" ]; then
  else wordcmp=$loan

  echo $1 (here print the correct file name passed by the caller)
  for FILE; do
    if [ -e $FILE ]; then
      cat $FILE | grep $wordcmp > $TMPFILE
      exec 3< $TMPFILE
  while read -u 3 line; do
    valore=$(($valore + ${line#$wordcmp}))
      exec 3>&-
      echo "$wordcmp $valore"
      echo "Bye."
      echo "$0: error --- log file $FILE not found" 1>&2

set -- `getopt -o "h" -l "query,loan" -- "$@"`

while true; do

   case "$1" in

         --query) query=QUERY

         --loan) loan=LOAN

         -h) echo "$0: help" 1>&2
         echo "usage: $0 --query logfile for query summary" 1>&2
             echo "usage: $0 --loan logfile for loan summary" 1>&2
             echo "usage: $0 -h for this help message" 1>&2
         exit 1;;
         --) shift

if [ -z "$query" -a -z "$loan" ]; then
  echo "$0: you must specify at least one option and log file(s)" 1>&2
  echo "please type $0 -h for help" 1>&2
  exit 1

if [ $# = 0 ]; then
  echo "$0: you must specify at least one log file" 1>&2
  echo "please type $0 -h for help" 1>&2
  exit 1
 count $@

The output when invoking myscript --query filename.log is

"./ error --- log file 'filename.log' not found"

(but the file "filename.log" exist on the local directory when the script is launched)

Any Idea?

Thanks to all

share|improve this question
did you try hard coding it to test? – AlanTuring Oct 1 '12 at 19:03

The error you say it gives ("./ error --- log file 'filename.log' not found") has single-quotes around the filename, but the command that prints it (echo "$0: error --- log file $FILE not found" 1>&2) didn't put them there. This suggests to me that they're part of the value of $FILE, meaning that it's looking for them as part of the actual file's name.

If I'm right about that, it's not a problem in the script, but in how you're calling it. So, how are you calling this script? If it's something like this:

cmd="./ --loan 'filename.log'"

...then that's the problem, because quoting in bash doesn't work that way; see BashFAQ #050: I'm trying to put a command in a variable, but the complex cases always fail.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.