在这个答案中,我只是对 Viktor 的代码做一些解释。
这个解释可能会帮助那些正在学习bash的人。在我发布“空间问题”部分之后以及在我完成编写并保存此解释之前,维克多更新了他的答案。这就是我所希望的。
非常简洁,有很好的解释。原来的代码是:
#!/bin/bash
#@filename: script.sh
find $1 -type f -name "*.$2"
听起来这个问题来自某种作业,我想这个原始代码可能满足作业的期望。它可能也适用于您将编写的大多数程序,如果问题不是来自作业,这一点尤其重要。
我经常在 Windows 或 Cygwin 上开发,所以我遇到了带有空格的路径和文件名。如果我在 Cygwin 上使用 @Viktor 的代码(请注意,当我在 linux 上使用 Viktor 的代码时会发生相同的行为)
$ pwd
/home/me
#Write the program
$ vim script.sh
#view the program
$ cat script.sh
#!/bin/bash
find $1 -type f -name "*.$2"
#Create a path with spaces in one of the directory names
#Name it after Viktor, for the great answer
CORP+dblack@CAP-D-ENG-INT3 ~
$ mkdir -p viktor/dir\ with\ spaces/directory
#put some css files in that directory
$ touch viktor/dir\ with\ spaces/directory/a.css
$ touch viktor/dir\ with\ spaces/directory/b.css
$ touch viktor/dir\ with\ spaces/directory/c.css
# run Viktor's code, using the well-written instructions
$ /bin/bash ./script.sh 'victor/dir with spaces/directory' css
find: ‘viktor/dir’: No such file or directory
find: ‘spaces/directory’: No such file or directory
请注意,我使用了./script.sh 而不是普通的script.sh,因为我没有将script.sh 设置为可执行。如果我第一次运行,我可以像 Viktor 显示的那样运行它
$ chmod +x script.sh
问题来自bash shell 和find 命令处理空格的方式。将第一个参数简单地作为$1 给出,空格不会被以特殊方式处理。这可能不是描述事物的最佳方式。也许解释这个概念的更好方法是表明bash 将分配以下内容。
$0='script.sh'
$1='viktor/dir with spaces/directory'
$2='css'
即使我们告诉bash我们的目录有空格也会发生这种情况
$ /bin/bash script.sh viktor/dir\ with\ spaces/directory css
find: ‘viktor/dir’: No such file or directory
find: ‘spaces/directory’: No such file or directory
这可以通过添加行来查看,echo "\$0 is $0"、echo "\$1 is $1"、echo "\$2 is $2",如果您愿意,还可以添加具有$(insert_number_here_but_no_parentheses) 的其他行。
那么,我们该如何解决这个问题呢?让我们看看find的手册页
$ man find
# (press 'q' to get out of the display of the man page)
好吧,那有点吓人。
让我们把原来的概要行删减到
find [starting-point...] [expression]
DESCRIPTON 在其“起点”解释中的意思是 [starting-point...] 是一个目录(或多个目录),您将从该目录开始搜索。现在,从表面上看,“表达式”是限制搜索的东西——在你的情况下,你只查找文件,然后通过指定它们具有特定的文件扩展名来进一步过滤结果。
让我们回到find $1 -type f -name "*.$2" 命令。这里要知道的重点是这里显示的命令会被展开为
find viktor/dir with spaces/directory -type f -name "*.css"
“空间问题”是find和shell会解释如下
find victor/dir with spaces/directory -type f -name "*.css"
| \ | \ `--------------------'
| \ | \ /
| | | \ Another '[expression]' (2)
| | | -- Another '[starting-point]'
| | -- '[expression]' (1)
| -- '[starting-point]' (supposedly a directory)
-- Command name
请注意,我们的 '[expression]' (1) 和 '[expression]' (2) 不会影响发生的情况,因为这两个 '[starting-place]' 路径失败。
现在 Viktor 的代码中显示的内容
#!/bin/bash
#@filename viktor_script.sh
find "$1" -type f -name "*.$2"
导致
find "viktor/dir with spaces/directory" -type f -name "*.css"
`--' `--------------------------------' `-------------------'
| | /
| | valid '[expression]' to define
| | a "filter" for files outputted
| |
| -- '[starting-point]' (a real directory)
| bash knows to include the spaces because
-- command name of the quotation marks
让我们运行它,首先确保它是可执行的
$ chmod +x viktor_script.sh
$ /bin/bash ./viktor_script.sh css
$ /bin/bash viktor_script.sh 'viktor/dir with spaces/directory' css
viktor/dir with spaces/directory/a.css
viktor/dir with spaces/directory/b.css
viktor/dir with spaces/directory/c.css