Как использовать 'если' внутреннее '-должностное-лицо' атрибут 'находки'?

У меня есть каталог со многими парами файлов. Каждый нетекстовый файл имеет текстового партнера по описанию. Например, каталог может быть похожим на это:

a.jpg
a.jpg.text
b.ogv
b.ogv.text
cd ef.JpG
cd ef.JpG.text

Я хочу подтвердить, что нет никаких свободных текстовых файлов (файлы описания чего-то уже удаленного). Таким образом, я попытался выполнить следующее:

find . -name '*.text' -exec if [ ! -f `basename -s .text {}` ]; then echo {}; fi \;

Однако теперь я получаю ошибку:

bash: syntax error near unexpected token `then'
3
задан 14 January 2015 в 23:58

2 ответа

Вы просто не можете использовать if в -exec действие find команда в пути можно пожелать. Это из-за следующих причин:

  • -exec действие ожидает команду. Это от man find (где-нибудь в строке 697 в моем терминале):
    -exec command ;
          Execute  command;  true  if 0 status is returned.  All following
          arguments to find are taken to be arguments to the command until
          an  argument  consisting of `;' is encountered.  The string `{}'
          is replaced by the current file name being processed  everywhere
          it occurs in the arguments to the command, not just in arguments
          where it is alone, as in some versions of find.  Both  of  these
          constructions might need to be escaped (with a `\') or quoted to
          protect them from expansion by the shell.  See the EXAMPLES sec‐
          tion for examples of the use of the -exec option.  The specified
          command is run once for each matched file.  The command is  exe‐
          cuted  in  the starting directory.   There are unavoidable secu‐
          rity problems surrounding use of the -exec  action;  you  should
          use the -execdir option instead.

    -exec command {} +
          This  variant  of the -exec action runs the specified command on
          the selected files, but the command line is built  by  appending
          each  selected file name at the end; the total number of invoca‐
          tions of the command will  be  much  less  than  the  number  of
          matched  files.   The command line is built in much the same way
          that xargs builds its command lines.  Only one instance of  `{}'
          is  allowed  within the command.  The command is executed in the
          starting directory.

Теперь, для выполнения, чего Вы желаете Вы не должны действительно использовать if в -exec. Просто сделайте тест в -exec, затем используйте -print (см. man find для большего количества информации):

find . -name '*.text' -exec $SHELL -c '[ ! -f ${1%.*} ]' $SHELL '{}' ';' -print

Иначе должен был бы использовать сценарий удара, следующим образом:

find . -name '*.text' -exec my_if {} \;

где cat ~/bin/my_if дает следующий вывод в моем случае:

#!/bin/bash
if [ ! -f $(basename -s .text "$1") ]; then echo "$1"; fi

Наконец, я думаю, что все нормальные люди, использующие удар, использовали бы:

for f in *.text; do if [ ! -f $(basename -s .text "$f") ]; then echo "$f"; fi; done
4
ответ дан 1 December 2019 в 13:25

Вот неоптимизированный способ использовать if в exec пункт:

find . -name '*.text' -exec sh -c \
    'if [ ! -f "$(dirname "$1")/$(basename "$1" .text)" ]; then echo == $1; fi' sh {} \;

и вот что-то намного лучше, которое не запускает одну оболочку на файл:

find . -name '*.text' -exec sh -c \
    'for i do if [ ! -f "${i%.text}" ]; then echo == $i; fi;done' sh {} +
3
ответ дан 1 December 2019 в 13:25

Другие вопросы по тегам:

Похожие вопросы: