Domanda

Vorrei leggere varie partizioni da un nastro DDS utilizzando uno script
Al momento sto usando il seguente script

TAPE=/dev/nst0         
BLOCK_SIZE=32768        
COUNTER=1
END_OF_DATA=120

while [  $COUNTER -lt $END_OF_DATA ] 
do      
       dd if=$TAPE bs=$BLOCK_SIZE of=file_$COUNTER  
       let COUNTER+=1 
done
.

Tuttavia, come puoi vedere il mio script può leggere a massimo 120 partizioni.

Come posso modificare questo codice per abilitare lo script di riconoscere la fine dei dati del nastro

Grazie

È stato utile?

Soluzione

È possibile continuare a confrontare il report con le dimensioni del blocco.E sì, devi aprire il dispositivo solo una volta e continuare a leggere da esso.

TAPE=/dev/nst0 
BLOCK_SIZE=32768
COUNTER=1

while [ "$(dd bs="$BLOCK_SIZE" of="file_$COUNTER" 2>&1 count=1 | awk '/bytes/ { print $1 }')" -eq "$BLOCK_SIZE" ]; do
    let ++COUNTER
done < "$TAPE"
.

Script è testato con un file.

Puoi anche rimuovere l'ultimo file se l'ultimo numero di byte Leggi è stato solo 0:

while
    BYTES_READ=$(dd bs="$BLOCK_SIZE" of="file_$COUNTER" 2>&1 count=1 | awk '/bytes/ { print $1 }')
    [ "$BYTES_READ" -eq "$BLOCK_SIZE" ]
do
    let ++COUNTER
done < "$TAPE"

[ "$BYTES_READ" -eq 0 ] && rm -f "file_$COUNTER"
.

Se si desidera inviare un messaggio mentre si elabora il nastro, è possibile utilizzare i reindirizzamenti e utilizzare un altro descrittore di file per questo:

TAPE=temp
BLOCK_SIZE=32768
COUNTER=1

while
    FILE="file_$COUNTER"
    echo "Reading $BLOCK_SIZE from $TAPE and writing it to file $FILE."
    BYTES_READ=$(dd bs="$BLOCK_SIZE" of="$FILE" count=1 2>&1 <&4 | awk '/bytes/ { print $1 }')
    echo "$BYTES_READ bytes read."
    [ "$BYTES_READ" -eq "$BLOCK_SIZE" ]
do
    let ++COUNTER
done 4< "$TAPE"

[ "$BYTES_READ" -eq 0 ] && rm -f "$FILE"
.

Esempio di uscita:

Reading 32768 from temp and writing it to file file_1.
32768 bytes read.
Reading 32768 from temp and writing it to file file_2.
32768 bytes read.
Reading 32768 from temp and writing it to file file_3.
32768 bytes read.
Reading 32768 from temp and writing it to file file_4.
32768 bytes read.
Reading 32768 from temp and writing it to file file_5.
32268 bytes read.
.

Un'altra opzione è semplicemente inviare i generatori generacodetagcode a echo.

echo "Reading $BLOCK_SIZE from $TAPE and writing it to file $FILE." >&2
.

Per renderlo un po 'più veloce, utilizzare /dev/stderr all'interno del subshell per evitare forcella extra:

BYTES_READ=$(exec dd ...)
.

Altri suggerimenti

Come ho detto in un commento, non sono molto esperto con questo problema, ma come "dd si arresta alla fine di ogni partizione" , perché non hai semplicemente letto fino a quando non ci sia semplicemente leggendoniente di più da leggere?

TAPE=/dev/nst0         
BLOCK_SIZE=32768        
COUNTER=1

while true 
do      
       dd if=$TAPE bs=$BLOCK_SIZE of=file_$COUNTER

       if [ \! -s file_$COUNTER ]
       then
               # clean-up empty file and exit the loop
               rm file_$COUNTER
               break
       fi
       let COUNTER+=1 
done
.

(non testato)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top