2009-02-11 17 views
9

Ho una parte di uno script bash che sta ottenendo un nome file senza estensione, ma sto cercando di capire cosa sta succedendo davvero qui. A cosa servono i "%%"? Qualcuno può approfondire cosa sta facendo bash dietro le quinte? Come può essere utilizzata questa tecnica su base generale?Usi per questa tecnica di estrazione di nome file bash?

#!/bin/bash 

for src in *.tif 
    do 
    txt=${src%%.*} 
    tesseract ${src} ${txt} 
    done 
+1

Prendere nota che %% rimuoverà tutto il "." nel nome del file, non solo la cosiddetta estensione. Ad esempio, se hai avuto hello.world.tif e hello.death.tif, entrambi avrebbero inviato a tesseract la stessa destinazione, ciao. Se si desidera rimuovere solo un'estensione, utilizzare%. – johnny

+0

Perché nel caso di più "." S nel nome del file che si desidera cercare e rimuovere il "modello di corrispondenza più breve" dal lato destro della stringa. Ha senso per me. Quindi nel caso del mio script sopra dovrei cambiarlo in "%". Questo dovrebbe eliminare le estensioni dei nomi dei file. – jjclarkson

risposta

15

E si libera della estensione del file (qui: .tif), del campione:

$ 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 

da manuale 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. 
+1

Dal tuo esempio posso iniziare a vedere quanto potente bash può essere giusto dalla riga di comando. Grazie! – jjclarkson

1

Controllare "Espansione parametri" nelle pagine man di bash. Questa sintassi espande la variabile $ src eliminando elementi che corrispondono allo schema. * Da esso.

+0

Grazie, non mi sono reso conto che le pagine man bash erano così estese. – jjclarkson

4

Ecco uscita dalla pagina man di 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. 
1

È un'operazione di rimozione di stringhe in mat: ${str%%substr}

Dove str è la stringa su cui si sta operando e substr è il modello da abbinare. Cerca la più lunga corrispondenza di substr in str e rimuove tutto da quel punto in poi.

3

Apparentemente bash ha diversi strumenti "parametri di espansione", che comprendono:

semplicemente sostituendo il valore ...

${parameter} 

Espansione ad un sub-string ...

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

Sostituire la lunghezza del valore dei parametri ...

${#parameter} 

espansione su di un match all'inizio del parametro ...

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

espansione su una partita alla fine del parametro ...

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

espande il parametro per trovare e sostituire una stringa ...

${parameter/pattern/string} 

Queste sono le mie interpretazioni delle parti che penso di capire da questa sezione delle pagine man. Fammi sapere se ho perso qualcosa di importante.

+0

Ti rendi semplicemente conto che c'è una leggera differenza tra # e ## e% e %%. Queste espansioni inoltre non comprendono i nomi dei file. – johnny