J'ai une partie d'un script bash qui devientun nom de fichier sans extension, mais j'essaie de comprendre ce qui se passe vraiment ici. À quoi servent les "%%" "? Quelqu'un peut-il expliquer ce que fait bash dans les coulisses? Comment cette technique peut-elle être utilisée de manière générale?
#!/bin/bash
for src in *.tif
do
txt=${src%%.*}
tesseract ${src} ${txt}
done
Réponses:
15 pour la réponse № 1Il se débarrasse de l'extension du nom de fichier (ici: .tif
), échantillon:
$ 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
de bash manuel:
${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 pour la réponse № 2
Voici le résultat de la page de manuel 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 pour la réponse № 3
Apparemment bash a plusieurs "Extension de paramètres"outils qui incluent:
Il suffit de substituer la valeur ...
${parameter}
Développer une sous-chaîne ...
${parameter:offset}
${parameter:offset:length}
Remplacez la longueur de la valeur des paramètres ...
${#parameter}
Développer une correspondance au début du paramètre ...
${parameter#word}
${parameter##word}
Développer une correspondance à la fin du paramètre ...
${parameter%word}
${parameter%%word}
Développe le paramètre pour rechercher et remplacer une chaîne ...
${parameter/pattern/string}
C’est mon interprétation des parties que je crois comprendre de cette partie des pages de manuel. Faites-moi savoir si j'ai manqué quelque chose d'important.
1 pour la réponse № 4
Consultez "Expansion des paramètres" dans les pages de manuel de bash. Cette syntaxe développe la variable $ src en supprimant les éléments qui correspondent au modèle. *.
1 pour la réponse № 5
C’est une opération de suppression de chaîne au format: ${str%%substr}
Où str est la chaîne sur laquelle vous travaillez, et substr est le motif à comparer. Il recherche la plus longue correspondance de substr dans str et supprime tout à partir de là.