add arguments, help, bugfixing
This commit is contained in:
126
split-dl.sh
126
split-dl.sh
@@ -1,16 +1,59 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
FILE="$1"
|
SPLITSIZE="10M" # Desired size of splitted files
|
||||||
TYPE="$2"
|
INFO="info.cfg" # Desired name of document containing information
|
||||||
SPLITSIZE="10M"
|
CHECKSUM="md5sum" # Define application to create hashsums
|
||||||
INFO="info.cfg"
|
FILE=""
|
||||||
|
MODE=""
|
||||||
|
|
||||||
# Check if 2 params are given
|
while getopts s:i:c:f:t:help: opt; do
|
||||||
if [ "$#" -lt "2" ]; then
|
case $opt in
|
||||||
echo "This script requires two arguments: File or Link AND server or client."
|
h)
|
||||||
|
echo "This program is used to split large files into several small
|
||||||
|
files to avoid data loss and the need to re-download them from
|
||||||
|
slow or unstable internet connections."
|
||||||
|
echo
|
||||||
|
echo "Necessary arguments to start this application:"
|
||||||
|
echo "-t Desired MODE. Possible values are \"server\" or \"client\""
|
||||||
|
echo " \"server\" splits the file on the server and needs a file for -f."
|
||||||
|
echo " \"client\" downloads the files and needs a download link for -f."
|
||||||
|
echo "-f File in the same directory to be splitted or link to be downloaded."
|
||||||
|
echo
|
||||||
|
echo "Optional arguments:"
|
||||||
|
echo "-s Define the desired size of splitted files. Default is $SPLITSIZE"
|
||||||
|
echo "-i Define the desired name of the info document. Default is $INFO"
|
||||||
|
echo "-c Define the program which should be used to calculate the hashsum. Default is $CHECKSUM"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
s)
|
||||||
|
SPLITSIZE=$OPTARG
|
||||||
|
;;
|
||||||
|
i)
|
||||||
|
INFO=$OPTARG
|
||||||
|
;;
|
||||||
|
c)
|
||||||
|
CHECKSUM=$OPTARG
|
||||||
|
;;
|
||||||
|
f)
|
||||||
|
FILE=$OPTARG
|
||||||
|
;;
|
||||||
|
m)
|
||||||
|
MODE=$OPTARG
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# DO NOT EDIT BELOW HERE
|
||||||
|
# ----------------------
|
||||||
|
|
||||||
|
if [ "$FILE" == "" ] || [ "$MODE" == "" ]; then
|
||||||
|
echo "Missing arguments! Please define the mode with -m and the file/link with -f. More info with -h."
|
||||||
exit 1
|
exit 1
|
||||||
|
else
|
||||||
|
echo "OK"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# # # #
|
# # # #
|
||||||
# FUNCTIONS
|
# FUNCTIONS
|
||||||
# # # #
|
# # # #
|
||||||
@@ -18,12 +61,12 @@ function checkwait {
|
|||||||
read -p "Continue? Press Ctrl+C to cancel." END
|
read -p "Continue? Press Ctrl+C to cancel." END
|
||||||
}
|
}
|
||||||
|
|
||||||
function getmd5 {
|
function gethash {
|
||||||
md5sum $1 | awk -F" " '{ print $1 }'
|
md5sum $1 | awk -F" " '{ print $1 }'
|
||||||
}
|
}
|
||||||
|
|
||||||
function compmd5 {
|
function comphash {
|
||||||
HASH1=$(getmd5 $1)
|
HASH1=$(gethash $1)
|
||||||
HASH2=$2
|
HASH2=$2
|
||||||
if [ "$HASH1" == "$HASH2" ]; then
|
if [ "$HASH1" == "$HASH2" ]; then
|
||||||
echo "true"
|
echo "true"
|
||||||
@@ -49,8 +92,8 @@ function compdu {
|
|||||||
function compall {
|
function compall {
|
||||||
FILE=$1
|
FILE=$1
|
||||||
SIZE=$2
|
SIZE=$2
|
||||||
MD5=$3
|
HASH=$3
|
||||||
if $(compdu $FILE $SIZE) && $(compmd5 $FILE $MD5); then
|
if $(compdu $FILE $SIZE) && $(comphash $FILE $HASH); then
|
||||||
echo="true"
|
echo="true"
|
||||||
else
|
else
|
||||||
echo="false"
|
echo="false"
|
||||||
@@ -66,7 +109,7 @@ function checkfolder {
|
|||||||
rm -rf $FOLDER
|
rm -rf $FOLDER
|
||||||
mkdir $FOLDER
|
mkdir $FOLDER
|
||||||
else
|
else
|
||||||
if [ "$TYPE" == "server" ]; then
|
if [ "$MODE" == "server" ]; then
|
||||||
echo "In server-mode, this isn't recommended. Aborting."
|
echo "In server-mode, this isn't recommended. Aborting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@@ -83,7 +126,7 @@ function checkfolder {
|
|||||||
# # # #
|
# # # #
|
||||||
# SERVER MODE
|
# SERVER MODE
|
||||||
# # # #
|
# # # #
|
||||||
if [ $TYPE == "server" ]; then
|
if [ $MODE == "server" ]; then
|
||||||
|
|
||||||
# Check if file has spaces
|
# Check if file has spaces
|
||||||
if [ $(echo "$FILE" | grep -q " "; echo $?) == "0" ]; then
|
if [ $(echo "$FILE" | grep -q " "; echo $?) == "0" ]; then
|
||||||
@@ -124,20 +167,20 @@ checkfolder $FOLDER
|
|||||||
INFONAME=$INFO
|
INFONAME=$INFO
|
||||||
INFO="$FOLDER/$INFONAME"
|
INFO="$FOLDER/$INFONAME"
|
||||||
|
|
||||||
echo "[INFO] Calculating size and MD5 sum..."
|
echo "[INFO] Calculating size and HASH sum..."
|
||||||
BIGNAME=$(basename $FILE)
|
BIGNAME=$(basename $FILE)
|
||||||
BIGSIZE=$(getdu $FILE)
|
BIGSIZE=$(getdu $FILE)
|
||||||
BIGMD5=$(getmd5 $FILE)
|
BIGHASH=$(gethash $FILE)
|
||||||
|
|
||||||
echo "BIGNAME=$BIGNAME" >> $INFO
|
echo "BIGNAME=$BIGNAME" >> $INFO
|
||||||
echo "BIGSIZE=$BIGSIZE" >> $INFO
|
echo "BIGSIZE=$BIGSIZE" >> $INFO
|
||||||
echo "BIGMD5=$BIGMD5" >> $INFO
|
echo "BIGHASH=$BIGHASH" >> $INFO
|
||||||
echo >> $INFO
|
echo >> $INFO
|
||||||
|
|
||||||
echo "[INFO] Splitting big file into smaller parts..."
|
echo "[INFO] Splitting big file into smaller parts..."
|
||||||
split --verbose -a 4 -b $SPLITSIZE $FILE $FOLDER/dl-
|
split --verbose -a 4 -b $SPLITSIZE $FILE $FOLDER/dl-
|
||||||
|
|
||||||
# List all splitted files, measure size and md5sum
|
# List all splitted files, measure size and hashsum
|
||||||
echo "[INFO] Creating info document with necessary specs..."
|
echo "[INFO] Creating info document with necessary specs..."
|
||||||
SMALLNO=$(ls $FOLDER | grep -v $INFONAME | wc -l)
|
SMALLNO=$(ls $FOLDER | grep -v $INFONAME | wc -l)
|
||||||
echo "SMALLNO=$SMALLNO" >> $INFO
|
echo "SMALLNO=$SMALLNO" >> $INFO
|
||||||
@@ -146,11 +189,11 @@ ls $FOLDER | grep -v $INFONAME | while read -r line; do
|
|||||||
|
|
||||||
SMALLNAME=$line
|
SMALLNAME=$line
|
||||||
SMALLSIZE=$(getdu $FOLDER/$SMALLNAME)
|
SMALLSIZE=$(getdu $FOLDER/$SMALLNAME)
|
||||||
SMALLMD5=$(getmd5 $FOLDER/$SMALLNAME)
|
SMALLHASH=$(gethash $FOLDER/$SMALLNAME)
|
||||||
|
|
||||||
echo "SMALLNAME[$NO]=$SMALLNAME" >> $INFO
|
echo "SMALLNAME[$NO]=$SMALLNAME" >> $INFO
|
||||||
echo "SMALLSIZE[$NO]=$SMALLSIZE" >> $INFO
|
echo "SMALLSIZE[$NO]=$SMALLSIZE" >> $INFO
|
||||||
echo "SMALLMD5[$NO]=$SMALLMD5" >> $INFO
|
echo "SMALLHASH[$NO]=$SMALLHASH" >> $INFO
|
||||||
|
|
||||||
let NO=NO+1
|
let NO=NO+1
|
||||||
done
|
done
|
||||||
@@ -164,7 +207,7 @@ fi # /SERVER MODE
|
|||||||
# # # #
|
# # # #
|
||||||
# CLIENT MODE
|
# CLIENT MODE
|
||||||
# # # #
|
# # # #
|
||||||
if [ $TYPE == "client" ]; then
|
if [ $MODE == "client" ]; then
|
||||||
|
|
||||||
URL=$FILE
|
URL=$FILE
|
||||||
|
|
||||||
@@ -177,6 +220,9 @@ fi
|
|||||||
if [ -e $INFO ]; then rm $INFO; fi
|
if [ -e $INFO ]; then rm $INFO; fi
|
||||||
wget -q $URL/$INFO
|
wget -q $URL/$INFO
|
||||||
|
|
||||||
|
# Rename info.cfg to avoid colissions and source it
|
||||||
|
mv $INFO $BIGNAME-$INFO
|
||||||
|
INFO=$BIGNAME-$INFO
|
||||||
source $INFO
|
source $INFO
|
||||||
|
|
||||||
FOLDER="dl-$BIGNAME"
|
FOLDER="dl-$BIGNAME"
|
||||||
@@ -184,7 +230,7 @@ FOLDER="dl-$BIGNAME"
|
|||||||
# Print basic status
|
# Print basic status
|
||||||
echo
|
echo
|
||||||
echo "Total filesize: $BIGSIZE"
|
echo "Total filesize: $BIGSIZE"
|
||||||
echo "MD5: $BIGMD5"
|
echo "HASH: $BIGHASH"
|
||||||
echo "Number of splitted files: $SMALLNO"
|
echo "Number of splitted files: $SMALLNO"
|
||||||
echo "Download folder: "$(readlink -f $FOLDER)""
|
echo "Download folder: "$(readlink -f $FOLDER)""
|
||||||
echo
|
echo
|
||||||
@@ -197,7 +243,7 @@ until [ "$STATUS" == "F" ]; do
|
|||||||
for ((i = 0; i < ${#SMALLNAME[*]}; i++)); do
|
for ((i = 0; i < ${#SMALLNAME[*]}; i++)); do
|
||||||
SMALLNAME=${SMALLNAME[$i]}
|
SMALLNAME=${SMALLNAME[$i]}
|
||||||
SMALLSIZE=${SMALLSIZE[$i]}
|
SMALLSIZE=${SMALLSIZE[$i]}
|
||||||
SMALLMD5=${SMALLMD5[$i]}
|
SMALLHASH=${SMALLHASH[$i]}
|
||||||
|
|
||||||
SMALLURL=$URL/$SMALLNAME
|
SMALLURL=$URL/$SMALLNAME
|
||||||
SMALLPATH=$FOLDER/$SMALLNAME
|
SMALLPATH=$FOLDER/$SMALLNAME
|
||||||
@@ -213,8 +259,8 @@ until [ "$STATUS" == "F" ]; do
|
|||||||
# File doesn't exist yet, so start fresh download
|
# File doesn't exist yet, so start fresh download
|
||||||
if [ ! -e $SMALLPATH ]; then
|
if [ ! -e $SMALLPATH ]; then
|
||||||
echo "[INFO] File doesn't exist yet. Starting new download."
|
echo "[INFO] File doesn't exist yet. Starting new download."
|
||||||
wget -O $SMALLPATH $SMALLURL
|
wget -O $SMALLPATH -nv --show-progress $SMALLURL
|
||||||
if $(compall $SMALLPATH $SMALLSIZE $SMALLMD5); then
|
if $(compall $SMALLPATH $SMALLSIZE $SMALLHASH); then
|
||||||
STATUS="C"
|
STATUS="C"
|
||||||
else
|
else
|
||||||
# Downloaded file is not valid. Restart for-loop
|
# Downloaded file is not valid. Restart for-loop
|
||||||
@@ -226,8 +272,8 @@ until [ "$STATUS" == "F" ]; do
|
|||||||
# File already exists but not finished yet
|
# File already exists but not finished yet
|
||||||
elif [ -e $SMALLPATH ] && ! $(compdu $SMALLPATH $SMALLSIZE); then
|
elif [ -e $SMALLPATH ] && ! $(compdu $SMALLPATH $SMALLSIZE); then
|
||||||
echo "[INFO] Continuing download."
|
echo "[INFO] Continuing download."
|
||||||
wget -c -O $SMALLPATH $SMALLURL
|
wget -c -O $SMALLPATH -nv --show-progress $SMALLURL
|
||||||
if $(compall $SMALLPATH $SMALLSIZE $SMALLMD5); then
|
if $(compall $SMALLPATH $SMALLSIZE $SMALLHASH); then
|
||||||
STATUS="C"
|
STATUS="C"
|
||||||
else
|
else
|
||||||
# Downloaded file is not valid. Restart for-loop
|
# Downloaded file is not valid. Restart for-loop
|
||||||
@@ -236,14 +282,14 @@ until [ "$STATUS" == "F" ]; do
|
|||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# File already exists, has correct size, but has wrong MD5
|
# File already exists, has correct size, but has wrong HASH
|
||||||
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH SMALLSIZE) && ! $(compmd5 $SMALLPATH $SMALLMD5); then
|
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH SMALLSIZE) && ! $(comphash $SMALLPATH $SMALLHASH); then
|
||||||
echo "[ERROR] MD5 is different but file has same size."
|
echo "[ERROR] HASH is different but file has same size."
|
||||||
echo "[INFO] Deleting file and starting new download."
|
echo "[INFO] Deleting file and starting new download."
|
||||||
checkwait
|
checkwait
|
||||||
rm $SMALLPATH
|
rm $SMALLPATH
|
||||||
wget -O $SMALLPATH $SMALLURL
|
wget -O $SMALLPATH -nv --show-progress $SMALLURL
|
||||||
if $(compall $SMALLPATH $SMALLSIZE $SMALLMD5); then
|
if $(compall $SMALLPATH $SMALLSIZE $SMALLHASH); then
|
||||||
STATUS="C"
|
STATUS="C"
|
||||||
else
|
else
|
||||||
# Downloaded file is not valid. Restart for-loop
|
# Downloaded file is not valid. Restart for-loop
|
||||||
@@ -252,8 +298,8 @@ until [ "$STATUS" == "F" ]; do
|
|||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# File already exists, has correct size, and has correct MD5
|
# File already exists, has correct size, and has correct HASH
|
||||||
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH $SMALLSIZE) && $(compmd5 $SMALLPATH $SMALLMD5); then
|
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH $SMALLSIZE) && $(comphash $SMALLPATH $SMALLHASH); then
|
||||||
echo "[SUCCESS] File already exists and is valid."
|
echo "[SUCCESS] File already exists and is valid."
|
||||||
STATUS="C"
|
STATUS="C"
|
||||||
|
|
||||||
@@ -295,9 +341,9 @@ if [ $YN == "y" ]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check big file for MD5 and size
|
# Check big file for HASH and size
|
||||||
sleep 2 # In rare cases, this can prevent a wrong du size
|
sleep 2 # In rare cases, this can prevent a wrong du size
|
||||||
echo "[INFO] Checking correct size and MD5 hashsum for rebuilt big file..."
|
echo "[INFO] Checking correct size and HASH hashsum for rebuilt big file..."
|
||||||
|
|
||||||
# Compare sizes
|
# Compare sizes
|
||||||
if $(compdu $BIGNAME $BIGSIZE); then
|
if $(compdu $BIGNAME $BIGSIZE); then
|
||||||
@@ -306,11 +352,11 @@ else
|
|||||||
echo "[ERROR] The size of the completed file is incorrect."
|
echo "[ERROR] The size of the completed file is incorrect."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Compare MD5 sum
|
# Compare HASH sum
|
||||||
if $(compmd5 $BIGNAME $BIGMD5); then
|
if $(comphash $BIGNAME $BIGHASH); then
|
||||||
echo "[SUCCESS] The MD5 sum of the completed file is corrent."
|
echo "[SUCCESS] The HASH sum of the completed file is corrent."
|
||||||
else
|
else
|
||||||
echo "[ERROR] The MD5 sum of the completed file is incorrect."
|
echo "[ERROR] The HASH sum of the completed file is incorrect."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user