shell dd命令在bs参数太大的时候出现异常的解决方法

14 篇文章 1 订阅

上文讲到了shell dd命令在bs参数太大的时候会出现异常。
现在讲讲怎么解决。
如果需要dd很大的文件的时候,需要指定很大的bs参数的时候怎么办呢?

答案是分块dd。多进行几次dd。

我写了个脚本sdd.sh,
sdd $file_input $file_output $bs
对应于
dd if=$file_input of=$file_output bs=$bs count=1

其中$bs值可以大于、等于、或小于dd的bs限定大小2147479552 (0x7ffff000)。

上脚本sdd.sh

#!/bin/bash
#
# Copyright 2019 peggy All rights reserved
# Author peggy.o_o@foxmail.com
# Created on 2019-4-25
# Version 1.0
# Title: super dd for <s> too large in 'dd if=<f> of=<o> bs=<s> count=1'
# you may not use this file until keep the declaraction aboving.

# dd if=f of=o bs=s count=1
f=$1
o=$2
s=$3

s_remaining=$s
s_finished=0
if [ $# -gt 3 ] ; then
    s_block=$4
else
    # 8M, infact 0.5M | 1M | 2M | 4M |8M are ok
    s_block=8388608
fi
echo "s_block : $s_block"
ls -al $f
action_times=0
if [ $s_remaining -gt $s_block ] ; then
    # $bs太大,需要分块dd
    index=0
    while [ $s_remaining -gt $s_block ] ; 
    do
        let action_times=action_times+1
        echo "++ dd times : $action_times"
        time dd if=$f of=$o bs=$s_block count=1 skip=$index seek=$index
        let s_remaining=s_remaining-$s_block
        let s_finished=s_finished+$s_block
        let index=index+1
    done
    let action_times=action_times+1
    echo "++ dd times : $action_times"
    time dd if=$f of=$o bs=1  count=$s_remaining skip=$s_finished seek=$s_finished
else
    # $bs不大,可以直接dd
    echo "bs $s gt 1g $s_block, normal"
    let action_times=action_times+1
    echo "++ dd times : $action_times"
    time dd if=$f of=$o bs=$s count=1
fi
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值