bash 检查文件列表中的文件是否存在于某个目录中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14174230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:10:13  来源:igfitidea点击:

Check whether files in a file list exist in a certain directory

filebashunixscripting

提问by user29772

The runtime arguments are as follows: $1is the path to the file containing the list of files $2is the path to the directory containing the files What I want to do is check that each file listed in $1exists in the $2directory

运行时参数如下:$1在路径包含文件的列表中的文件 $2的路径包含我想要做的文件的目录检查,在列出的每个文件$1中存在$2目录

I'm thinking something like:

我在想这样的事情:

for f in 'cat '
  do
  if (FILEEXISTSINDIRECTORY)
    then echo '$f exists in '
    else echo '$f is missing in ' sleep 5 exit
  fi
done

As you can see, I want it so that if any of the files listed in $1don't exist in $2directory, the script states this then closes. The only part I can't get my head around is the (FILEEXISTSIN$2DIRECTORY) part. I know that you can do [ -e $f ], but I don't know how you can make sure its checking that it exists in the $2 directory.

如您所见,我希望这样,如果目录中$1不存在 中列出的任何文件$2,则脚本会声明该文件然后关闭。我唯一无法理解的部分是 (FILEEXISTSIN$2DIRECTORY) 部分。我知道你可以做[ -e $f ],但我不知道你如何确保它检查它是否存在于 $2 目录中。

Edit: Thinking further upon this, perhaps I could use nested for loops?

编辑:进一步思考这个问题,也许我可以使用嵌套的 for 循环?

回答by Josh Cartwright

If your specified input file contains a newline-separated list of files to check, then the following solution (using a while read loop) is robust enough to handle file names with spaces properly.

如果您指定的输入文件包含要检查的以换行符分隔的文件列表,那么以下解决方案(使用 while 读取循环)足够强大,可以正确处理带空格的文件名。

Generally, you should never make use of a loop of the form for i in $(command), and instead opt for a while loop. See http://mywiki.wooledge.org/DontReadLinesWithForfor more details.

通常,您永远不应使用 形式的循环for i in $(command),而应选择 while 循环。有关更多详细信息,请参阅http://mywiki.wooledge.org/DontReadLinesWithFor

while read -r file; do
   if [[ -e "/$file" ]]; then
      echo "$f exists in "
   else
      echo "$f does not exist in "
      sleep 5
      exit 1
   fi
done < ""

回答by Jonathan Leffler

Since you're dealing with a list of file names without spaces in the names (because the $(cat $1)notation will split things up like that), it is relatively straight forward:

由于您正在处理名称中没有空格的文件名列表(因为$(cat $1)符号会像那样拆分内容),因此相对简单:

for file in $(cat )
do
    if [ -e "/$file" ]
    then echo "$file exists in "
    else echo "$file is missing in "; sleep 5; exit 1
    fi
done

Basically, use the built-in string concatenation facilities to build the full path to the file, and use the testor [operator to check the files existence.

基本上,使用内置的字符串连接工具构建文件的完整路径,并使用testor[运算符检查文件是否存在。

The complexities arise if you have to deal with arbitrary file names, especially if one of the arbitrary characters in an arbitrary file name can be the newline character. Suffice to say, they complicate the issue sufficiently that I won't deal with it unless you say you need it dealt with, and even then, I'll negotiate on whether newlines in names need to be handled. The double-quoted variable expansion is a key part of the strategy for dealing with it. The other part of the problem is how to get the file names accurately into a variable.

如果您必须处理任意文件名,就会出现复杂性,尤其是当任意文件名中的任意字符之一可以是换行符时。我只想说,它们使问题变得足够复杂,除非您说需要处理,否则我不会处理它,即使如此,我也会协商是否需要处理名称中的换行符。双引号变量扩展是处理它的策略的关键部分。问题的另一部分是如何将文件名准确地放入变量中。