您可以继续将报告与块大小进行比较。是的,您只需打开设备一次并继续阅读。
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
在 subshell 内部使用以防止额外的分叉:
BYTES_READ=$(exec dd ...)