私はいくつかのファイルに使用しようとfind
しecho 0
ていますが、明らかにこれはsh -c
:
find /proc/sys/net/ipv6 -name accept_ra -exec sh -c 'echo 0 > {}' \;
しかし、sh -c
with find -exec
を使用すると、引用の問題が疑われるため、非常に不安になります。私はそれを少しいじりましたが、明らかに私の疑念は正当化されました:
私のテスト設定:
martin@dogmeat ~ % cd findtest martin@dogmeat ~/findtest % echo one > file\ with\ spaces martin@dogmeat ~/findtest % echo two > file\ with\ \'single\ quotes\' martin@dogmeat ~/findtest % echo three > file\ with\ \"double\ quotes\" martin@dogmeat ~/findtest % ll insgesamt 12K -rw-rw-r-- 1 martin martin 6 Sep 17 12:01 file with "double quotes" -rw-rw-r-- 1 martin martin 4 Sep 17 12:01 file with 'single quotes' -rw-rw-r-- 1 martin martin 4 Sep 17 12:01 file with spaces
find -exec
without を使用してもsh -c
問題なく動作するようです。ここでは引用符は不要です。martin@dogmeat ~ % find findtest -type f -exec cat {} \; one two three
しかし、私が使用して
sh -c
{}
いるときは、ある種の引用が必要なようです:martin@dogmeat ~ % LANG=C find findtest -type f -exec sh -c 'cat {}' \; cat: findtest/file: No such file or directory cat: with: No such file or directory cat: spaces: No such file or directory cat: findtest/file: No such file or directory cat: with: No such file or directory cat: single quotes: No such file or directory cat: findtest/file: No such file or directory cat: with: No such file or directory cat: double quotes: No such file or directory
ファイル名に二重引用符が含まれていない限り、二重引用符は機能します。
martin@dogmeat ~ % LANG=C find findtest -type f -exec sh -c 'cat "{}"' \; one two cat: findtest/file with double: No such file or directory cat: quotes: No such file or directory
ファイル名に単一引用符が含まれていない限り、単一引用符は機能します。
martin@dogmeat ~ % LANG=C find findtest -type f -exec sh -c "cat '{}'" \; one cat: findtest/file with single: No such file or directory cat: quotes: No such file or directory three
すべてのケースで機能するソリューションは見つかりませんでした。私が見落としているもの、または本質的に安全ではないものを使用sh -c
していfind -exec
ますか?
sh
はある種のプレースホルダーのよう_
です。たとえば、bash internalsを呼び出したい場合に非常に便利ですfind /tmp -name 'fil*' -exec bash -c 'printf "%q\n" "$1"' _ {} \;
。しかし、誰がこれが文書化されているか知っていますか?