Подтвердить что ты не робот

Используется для этого метода извлечения файлов bash?

У меня есть часть bash script, которая получает имя файла без расширения, но я пытаюсь понять, что здесь происходит. Что такое "%%"? Может кто-то уточнить, что делает bash за кулисами? Как можно использовать эту технику на общей основе?

#!/bin/bash

for src in *.tif
    do
    txt=${src%%.*}
    tesseract ${src} ${txt}
    done
4b9b3361

Ответ 1

Он избавляется от расширения имени файла (здесь: .tif), sample:

$ for A in test.py test.sh test.xml test.xsl; do echo "$A: ${A%%.*}"; done
test.py: test
test.sh: test
test.xml: test
test.xsl: test

из bash руководства:

   ${parameter%%word}
          The word is expanded to produce a pattern just as in pathname expansion.  If the
          pattern matches a trailing portion of the expanded value of parameter, then  the
          result  of  the  expansion  is the expanded value of parameter with the shortest
          matching pattern (the ``%'' case) or the longest matching  pattern  (the  ``%%''
          case) deleted.  If parameter is @ or *, the pattern removal operation is applied
          to each positional parameter in turn, and the expansion is the  resultant  list.
          If  parameter  is an array variable subscripted with @ or *, the pattern removal
          operation is applied to each member of the array in turn, and the  expansion  is
          the resultant list.

Ответ 2

Здесь выводится справочная страница bash

 ${parameter%%word}
          The word is expanded to produce a pattern just  as  in  pathname
          expansion.   If  the  pattern  matches a trailing portion of the
          expanded value of parameter, then the result of the expansion is
          the  expanded value of parameter with the shortest matching pat-
          tern (the ``%'' case)  or  the  longest  matching  pattern  (the
          ``%%''  case)  deleted.   If  parameter  is  @ or *, the pattern
          removal operation is applied to  each  positional  parameter  in
          turn,  and the expansion is the resultant list.  If parameter is
          an array variable subscripted with @ or *, the  pattern  removal
          operation  is  applied  to each member of the array in turn, and
          the expansion is the resultant list.

Ответ 3

По-видимому, bash имеет несколько инструментов Расширение параметра, которые включают:

Просто подставив значение...

${parameter}

Расширение подстроки...

${parameter:offset}
${parameter:offset:length}

Замените длину значения параметров...

${#parameter}

Развертывание по совпадению в начале параметра...

${parameter#word}
${parameter##word}

Развертывание по совпадению в конце параметра...

${parameter%word}
${parameter%%word}

Развертывает параметр для поиска и замены строки...

${parameter/pattern/string}

Это моя интерпретация тех частей, которые, я думаю, я понимаю из этого раздела страниц руководства. Дайте мне знать, если я пропустил что-то важное.

Ответ 4

Проверьте "Расширение параметров" на страницах bash. Этот синтаксис расширяет переменную $src, удаляя материал, соответствующий шаблону. *.

Ответ 5

Это операция удаления строки в формате: ${str%%substr}

Где str - строка, в которой вы работаете, а substr - шаблон, который нужно сопоставить. Он ищет самое длинное совпадение substr в str и удаляет все с этой точки.