bash - Shell script error using command find

06
2013-08
  • Anson

    My script is supposed to: recursively look for files and directories whose names contain given strings, using find, starting in the current directory. If no arguments are given the message Missing argument(s) shall be printed before returning the error code 1 to the shell.

    This is the script:

    #!/bin/bash/
    if ["${#}" -eq 0]
      then
         echo "Missing argument(s)"
         exit 1
    else
      find .  -name "*$@*"
    fi
    

    I tried to use this in the terminal, but I got this error: bash: ./myfind: /bin/bash/: bad interpreter : Not a directory

    Why? Is my code correct?

  • Answers
  • Paul Richter

    You need to delete the slash at the end of the first line. /bin/bash is the interpreter.

  • Keith Thompson

    You also need a space between [ and ", and between 0 and ].

    Bonus: You can simplify "${#}" to just $# - There's no need to quote a variable which is always going to be a number, and there's no need to use braces unless you've got more than just a single variable within the string.


  • Related Question

    unix - find command exec option
  • brianegge

    I'm trying to use the + option of find exec instead of xargs. However, I can't seem to get it to work. I feel I must be missing something obvious here.

    Example:

    find . -name "*.java" -exec grep "@author" {} + \;
    find: paths must precede expression
    Usage: find [-H] [-L] [-P] [path...] [expression]
    

    On the other hand, this works:

    find . -name "*.java" -exec grep "@author" {}  \;
    

    as does this:

    find . -name "*.java" | xargs  grep "@author"
    

  • Related Answers
  • wRAR

    You don't need \; together with +

  • Ryan Thompson

    It's much easier just to use find -print0 | xargs --null -I XXX do_something XXX. Let find to the finding and xargs do the, er, other stuff.