如果find命令失败,Shell停止脚本

如果find命令失败,Shell停止脚本,shell,find,Shell,Find,你好 在fine脚本中,我有以下find命令: find -maxdepth 1 \! -type d -name "some_file_name_*" -name "*.txt" -name "*_${day_month}_*" -exec cp {} /FILES/directory1/directory2/directory3/ + 我想知道如果命令找不到任何内容,如何停止脚本。使用GNU xargs和-r开关以及管道,以确保find的输出仅在其非空时传递给cp find -maxdep

你好

在fine脚本中,我有以下find命令:

find -maxdepth 1 \! -type d -name "some_file_name_*" -name "*.txt" -name "*_${day_month}_*" -exec cp {} /FILES/directory1/directory2/directory3/ +

我想知道如果命令找不到任何内容,如何停止脚本。

使用
GNU xargs
-r
开关以及管道,以确保
find
的输出仅在其非空时传递给
cp

find -maxdepth 1 \! -type d -name "some_file_name_*" -name "*.txt" -name "*_${day_month}_*" \
       | xargs -r I{} cp "{}" /FILES/directory1/directory2/directory3/
I{}
是传递给
cp
find
命令输出的占位符

根据
man xargs
页面,标志
-r
I{}
表示以下内容:

-r, --no-run-if-empty
      If the standard input does not contain any nonblanks, do not run
      the command.  Normally, the command is run once even if there is
      no input.  This option is a GNU extension.

-I replace-str
       Replace occurrences of replace-str in the initial-arguments with
       names  read  from  standard input.  

使用带有
-r
开关和管道的
GNU xargs
,确保
find
的输出仅在其非空时传递给
cp

find -maxdepth 1 \! -type d -name "some_file_name_*" -name "*.txt" -name "*_${day_month}_*" \
       | xargs -r I{} cp "{}" /FILES/directory1/directory2/directory3/
I{}
是传递给
cp
find
命令输出的占位符

根据
man xargs
页面,标志
-r
I{}
表示以下内容:

-r, --no-run-if-empty
      If the standard input does not contain any nonblanks, do not run
      the command.  Normally, the command is run once even if there is
      no input.  This option is a GNU extension.

-I replace-str
       Replace occurrences of replace-str in the initial-arguments with
       names  read  from  standard input.  

您可以添加
-exec false{}
,以便在找到某个对象时获得一个错误的退出状态(这使它有点颠倒)


请参阅stackexchange中的类似问题:,特别是它建议使用
false
技巧您可以添加
-exec false{}
,这样在找到某个对象时,您会得到一个错误的退出状态(这使它有点颠倒)


请参阅stackexchange中的类似问题:,特别是其中建议的
false
技巧

我将尽快对此进行测试,并在可能的情况下向上投票我将尽快进行测试,在可能的情况下向上投票我现在正在电脑之外处理一些东西,我现在正在处理电脑以外的一些东西,回来后我会试试,如果它能工作,我会把它标记为正确。