首页 > 开发 > 综合 > 正文

如何用脚本实现分割文件

2024-07-21 02:35:20
字体:
来源:转载
供稿:网友

  #!/bin/bash
  if [ $# -ne 2 ]; then
   echo 'Usage: split file size(in bytes)'
   exit
  fi
  file=$1
  size=$2
  if [ ! -f $file ]; then
   echo "$file doesn't exist"
   exit
  fi
  #TODO: test if $size is a valid integer
  filesize=`/bin/ls -l $file awk '{PRint $5}'`
  echo filesize: $filesize
  let pieces=$filesize/$size
  let remain=$filesize-$pieces*$size
  if [ $remain -gt 0 ]; then
   let pieces=$pieces+1
  fi
  echo pieces: $pieces
  i=0
  while [ $i -lt $pieces ];
  do
   echo split: $file.$i:
   dd if=$file of=$file.$i bs=$size count=1 skip=$i
   let i=$i+1
  done
  echo "#!/bin/bash" > merge
  echo "i=0" >> merge
  echo "while [ $i -lt $pieces ];" >> merge
  echo "do" >> merge
  echo " echo merge: $file.$i" >> merge
  echo " if [ ! -f $file.$i ]; then" >> merge
  echo " echo merge: $file.$i missed" >> merge
  echo " rm -f $file.merged" >> merge
  echo " exit" >> merge
  echo " fi" >> merge
  echo " dd if=$file.$i of=$file.merged bs=$size count=1 seek=$i" >> merge
  echo " let i=$i+1" >> merge
  echo "done" >> merge
  chmod u+x merge'

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表