Skip to content

Instantly share code, notes, and snippets.

@codingChewie
Created November 19, 2020 14:46
Show Gist options
  • Save codingChewie/fe194f5064084d15c6a562ede1487f85 to your computer and use it in GitHub Desktop.
Save codingChewie/fe194f5064084d15c6a562ede1487f85 to your computer and use it in GitHub Desktop.
Bash shell script to unzip an ePub
#!/bin/bash
# date: 20-11-03
# dev: codingChewie
# purpose: Unzip an ePub with name passed in Terminal
# version: 1.1
# name: unzip-pub.sh
EXT=".epub"
LOC=""
OUT=""
red=`tput setaf 1`
green=`tput setaf 2`
blue=`tput setaf 4`
white=`tput setaf 7`
read -p "${blue}What is the filename?: " pub
echo $white
if [[ $pub =~ ${EXT}$ ]]; then pub=$(echo "$pub" | cut -f 1 -d '.'); fi
if [ -z "${LOC}" ]; then LOC="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"; fi
filepath=(${LOC}/${pub}${EXT})
if [[ ! -d ${OUT} && ! -n "$OUT" ]]; then OUT=$LOC; fi
if [ -e ${filepath} ]; then
for epubfile in $(find ${filepath}); do
cd $LOC
zipfile=$(basename $epubfile .epub).zip
directoryname=$(basename $zipfile .zip)
echo $green
mv -v "$epubfile" "$LOC"/"$zipfile"
ziperror=0
ziperror=$(unzip -t "$LOC"/"$zipfile" > /dev/null)$?
if [ $ziperror -eq "0" ]; then
unzip -o "$LOC"/"$zipfile" -d "$LOC"/"$directoryname"
rm -v "$LOC/$directoryname.zip"
fi
echo $white
exit
done
else
echo "${red}File \"${pub}\" does not exist in directory. Please try again"
echo $white
exit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment