Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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..

#!/bin/bash

query=
loan=

function count(){
  local valore=0
  local wordcmp
  TMPFILE=$(mktemp)

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

  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}))
  done
      exec 3>&-
      echo "$wordcmp $valore"
      echo "Bye."
    else
      echo "$0: error --- log file $FILE not found" 1>&2
   fi
   valore=0
  done
}

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

while true; do

   case "$1" in

         --query) query=QUERY
              shift;;

         --loan) loan=LOAN
             shift;;

         -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
break;;
 esac
done

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
fi

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
  shift
fi
 count $@

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

"./myscript.sh: 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
1  
did you try hard coding it to test? –  AlanTuring Oct 1 '12 at 19:03

1 Answer 1

The error you say it gives ("./myscript.sh: 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="./myscript.sh --loan 'filename.log'"
$cmd

...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

 
discard

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.