简体   繁体   English

在终端中找到作品但在bash脚本中失败

[英]find works in terminal but fails in bash script

So I'm writing a bash script that counts the number of files in a directory and outputs a number. 因此,我正在编写一个bash脚本,该脚本计算目录中文件的数量并输出一个数字。 The function takes a directory argument as well as an optional file-type extension argument. 该函数接受目录参数以及可选的文件类型扩展名参数。

I am using the following lines to set the dir variable to the directory and ext variable to a regular expression that will represent all the file types to count. 我正在使用以下几行将dir变量设置为目录,将ext变量设置为将表示要计数的所有文件类型的正则表达式。

dir=$1
[[ $# -eq 2 ]] && ext="*.$2" || ext="*"

The problem I am encountering occurs when I attempt to run the following line: 当我尝试运行以下行时,会遇到我遇到的问题:

echo $(find $dir -maxdepth 1 -type f -name $ext | wc -l)

Running the script from the terminal works when I provide the second file-type argument but fails when I don't. 当我提供第二个文件类型参数时,可以从终端运行脚本,但如果不提供则失败。

harrison@Luminous:~$ bash Documents/howmany.sh Documents/ sh
3
harrison@Luminous:~$ bash Documents/howmany.sh Documents/
find: paths must precede expression: Desktop
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
0

I have searched for this error and I know it's an issue with the shell expanding my wildcard as explained here . 我搜索了这个错误,我知道这是与外壳扩大我通配符作为解释的一个问题在这里 I've tried experimenting with single quotes, double quotes, and backslashes to escape the asterisk but nothing seems to work. 我尝试用单引号,双引号和反斜杠进行实验,以避开星号,但似乎没有任何效果。 What's particularly interesting is that when I try running this directly through the terminal, it works perfectly fine. 尤其有趣的是,当我尝试直接通过终端运行此程序时,它运行得很好。

harrison@Luminous:~$ echo $(find Documents/ -maxdepth 1 -type f -name "*" | wc -l)
6

Simplified: 简化:

dir=${1:-.}           #if $1 not set use .
name=${2+*.$2}        #if $2 is set use *.$2 for name
name=${name:-*}       #if name still isnt set, use *

find "$dir" -name "$name" -print   #use quotes

or 要么

name=${2+*.$2}        #if $2 is set use *.$2 for name
find "${1:-.}" -name "${name:-*}" -print   #use quotes

also, as @John Kugelman says, you could use: 另外,正如@John Kugelman所说,您可以使用:

 name=${2+*.$2}
 find "${1:-.}" ${name:+-name "$name"} -print

find . -name "*" -print find . -name "*" -print is the same as find . -print find . -name "*" -printfind . -print相同find . -print find . -print , so if $name isn't set, there's no need to specify -name "*" . find . -print ,因此如果未设置$name ,则无需指定-name "*"

Try this: 尝试这个:

dir="$1"
[[ $# -eq 2 ]] && ext='*.$2' || ext='*'

If that doesn't work, you can just switch to an if statement, where you use the -name pattern in a branch and you don't in the other. 如果这不起作用,则可以切换到if语句,在该语句中,您在分支中使用-name模式,而在另一个中则不使用。

A couple more points: 还有两点:

  • Those are not regular expressions , but rather shell patterns . 这些不是正则表达式 ,而是外壳模式
  • echo $(command) is just equivalent to command . echo $(command)等效于command

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM