0

我想使用脚本从 DDS 磁带中读取各种分区
目前我正在使用以下脚本

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

但是,如您所见,我的脚本最多可以读取 120 个分区。

如何修改此代码以使脚本能够自动识别磁带数据的结尾

谢谢

4

2 回答 2

2

您可以继续将报告与块大小进行比较。是的,您只需打开设备一次并继续阅读。

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"

使用文件测试脚本。

如果最后读取的字节数仅为 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"

如果您想在处理磁带时发送消息,您可以使用重定向并为其使用另一个文件描述符:

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"

示例输出:

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.

另一种选择是将您echo的 s 发送到/dev/stderr.

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

为了让它更快一点,exec在 subshel​​l 内部使用以防止额外的分叉:

BYTES_READ=$(exec dd ...)
于 2014-07-29T08:27:57.020 回答
1

正如我在评论中所说,我对这个问题不是很精通,但是作为dd在每个分区的末尾停止”,您为什么不简单地阅读,直到没有更多可阅读的内容?

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

(未经测试)

于 2014-07-29T11:26:41.553 回答