ฉันพยายามที่จะใช้findเพื่อecho 0เป็นไฟล์บาง แต่เห็นได้ชัดว่านี้จะทำงานเฉพาะกับsh -c:
find /proc/sys/net/ipv6 -name accept_ra -exec sh -c 'echo 0 > {}' \;
แต่การใช้งานsh -cด้วย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โดยไม่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น่าจะเป็นชนิดของตัวยึดบางส่วนก็ทำงานเกินไปถ้าแทนที่ด้วย_ตัวอย่างเช่น - มีประโยชน์มากถ้าคุณต้องการที่จะเรียก internalsfind /tmp -name 'fil*' -exec bash -c 'printf "%q\n" "$1"' _ {} \;ทุบตี: แต่ไม่มีใครรู้ว่าเอกสารนี้อยู่ที่ไหน?