私はSolaris10を使用しており、ksh(88)、bash(3.00)、zsh(4.2.1)を使用して以下をテストしました。
次のコードは結果を生成しません:
function foo {
echo "Hello World"
}
find somedir -exec foo ;
検索はいくつかのファイルと一致します(-exec ...を置き換えることで示されます) -printを使用 )、関数はfindから外部で呼び出されたときに完全に機能します 電話してください。
これがman find -execについてのページ :
-exec command True if the executed command returns a
zero value as exit status. The end of
command must be punctuated by an escaped
semicolon (;). A command argument {} is
replaced by the current pathname. If the
last argument to -exec is {} and you
specify + rather than the semicolon (;),
the command is invoked fewer times, with
{} replaced by groups of pathnames. If
any invocation of the command returns a
non-zero value as exit status, find
returns a non-zero exit status.
私はおそらくこのようなことをやめることができます:
for f in $(find somedir); do
foo
done
しかし、フィールドセパレータの問題に対処することを恐れています。
find ... -exec ...からシェル関数(同じスクリプトで定義されているので、スコープの問題を気にしないでください)を呼び出すことは可能ですか? 電話しますか?
/usr/bin/findの両方で試してみました および/bin/find 同じ結果が得られました。
承認された回答:
function はシェルに対してローカルであるため、find -execが必要になります シェルを生成し、使用する前にそのシェルでその関数を定義します。次のようなもの:
find ... -exec ksh -c '
function foo {
echo blan: "[email protected]"
}
foo "[email protected]"' ksh {} +
bash export -fを使用して環境を介して関数をエクスポートできるようにします 、(bashで)実行できるように:
foo() { ...; }
export -f foo
find ... -exec bash -c 'foo "[email protected]"' bash {} +
ksh88 typeset -fxがあります (環境を介さずに)関数をエクスポートしますが、これはkshによって実行されるより少ないスクリプトでのみ使用できます。 、ksh -cではありません 。
別のオプションは次のとおりです。
find ... -exec ksh -c "
$(typeset -f foo)"'
foo "[email protected]"' ksh {} +
つまり、typeset -fを使用します fooの定義をダンプします インラインスクリプト内で機能します。 fooの場合は注意してください 他の関数を使用する場合は、それらもダンプする必要があります。