Mám časť bash skriptu, ktorý sa dostávanázov súboru bez prípony, ale snažím sa pochopiť, čo sa tu naozaj deje. Na čo slúži%%? Môže niekto rozpracovať, čo bash robí v zákulisí? Ako je možné túto techniku použiť všeobecne?
#!/bin/bash
for src in *.tif
do
txt=${src%%.*}
tesseract ${src} ${txt}
done
odpovede:
15 pre odpoveď č. 1Zbaví sa prípony názvu súboru (tu: .tif
), vzorka:
$ 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
z bash manuálu:
${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.
4 pre odpoveď č. 2
Tu je výstup zo stránky bash man
${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 pre odpoveď № 3
Zrejme bash má niekoľko "Rozšírenie parametrov„nástroje, ktoré zahŕňajú:
Stačí nahradiť hodnotu ...
${parameter}
Expanduje sa na podreťazec ...
${parameter:offset}
${parameter:offset:length}
Nahraďte dĺžku hodnoty parametra ...
${#parameter}
Rozširuje sa pri zhode na začiatku parametra ...
${parameter#word}
${parameter##word}
Rozširuje sa po zhode na konci parametra ...
${parameter%word}
${parameter%%word}
Rozširuje parameter na nájdenie a nahradenie reťazca ...
${parameter/pattern/string}
Toto je moja interpretácia častí, o ktorých si myslím, že rozumiem z tejto časti manuálových stránok. Dajte mi vedieť, ak mi chýba niečo dôležité.
1 pre odpoveď č. 4
Pozrite sa na "Rozšírenie parametrov" na stránkach bash man. Táto syntax rozširuje premenné $ src, z ktorých sa odstraňujú veci, ktoré sa zhodujú so vzorom. *.
1 pre odpoveď č. 5
Je to operácia odstránenia reťazca vo formáte: ${str%%substr}
Kde str je reťazec, na ktorom pracujete, a subst je vzor, ktorý sa má zhodovať. Hľadá najdlhšiu zhodu substrátu v str a od tohto bodu odstráni všetko.