2017-02-06 20 views
0

おはよう。罰金のスクリプトでfindコマンドが失敗した場合のシェル停止スクリプト

私は、次のfindコマンドを持っている:

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

私は、コマンドが何かを見つけるdoes't場合、スクリプトを停止する方法を知りたいです。 findの出力を確保するために-rスイッチとパイプラインとの

+0

イムを示唆して、病気の私が戻るときにそれを試してみて、正しいとしてマークそれが動作すれば。 –

答えて

1

使用GNU xargscp場合にのみ、その非空に渡されます。

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コマンドからの出力のためのプレースホルダであり、

フラグは、-rI{}

-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. 
man xargsページに応じて、以下を表します
+0

私はできるだけ早くこれを試してみるつもりです –

1

-exec false {}を追加して、何かが見つかったときに偽の終了ステータスを取得することができます(ただし、これは逆さになります)。

if find . -name foo -exec echo ok ';' -exec false {} + 
then 
    echo 'not found' 
    exit 
fi 
echo found 

はstackexchangeで同様の質問を参照してください:How to detect whether “find” found any matches?、特にthis answer今すぐ私のPCからいくつかのものを扱ってfalseトリック

関連する問題