init terminator

This commit is contained in:
Simon Vieille 2021-04-27 11:25:55 +02:00
commit 9af5b4725b
2 changed files with 135 additions and 0 deletions

24
README.md Normal file
View File

@ -0,0 +1,24 @@
Excel password terminator
=========================
This script removes passwords in an Excel file.
Requirements
------------
* `zip` and `unzip`
* `grep`
* `sed`
On a Debian/Ubuntu system, simply run `sudo apt install zip unzip grep sed`.
Usage
-----
```
./bin/terminate -i excel_with_passwords.xlsx -o excel_without_password.xlsx
```
You can turn on verbosity using `-v`. Help is provided with `-h`.
[]!(https://upload.deblan.org/u/2021-04/6087d86b.jpg)

111
bin/terminate Executable file
View File

@ -0,0 +1,111 @@
#!/bin/sh
usage() {
cat << EOF
USAGE:
$0 [options]
OPTIONS:
-i Input file (required)
-o Output file (required)
-v Verbose
-h Show this help
EOF
}
error() {
if [ -n "$1" ]; then
printf "%s\\n" "$1" >/dev/stderr
fi
exit 1
}
exit_if_empty() {
if [ -z "$1" ]; then
error "$2"
fi
}
log() {
if [ "$VERBOSE" -eq 1 ]; then
printf "%s\\n" "$1"
fi
}
unzipExcel() {
if [ "$VERBOSE" -eq 0 ]; then
verbosity=-qq
fi
unzip $verbosity -d "$TEMP_DIRECTORY/excel" "$INPUT_FILE"
}
createExcel() {
current_dir="$(pwd)"
if [ "$VERBOSE" -eq 0 ]; then
verbosity=-q
fi
cd "$TEMP_DIRECTORY/excel"
zip $verbosity -r "../result.xslx" ./*
cd "$current_dir"
mv "$TEMP_DIRECTORY/result.xslx" "$OUTPUT_FILE"
log "$OUTPUT_FILE generated"
}
createTmpDirectory() {
TEMP_DIRECTORY="$(mktemp -d)"
log "Temp directory created: $TEMP_DIRECTORY"
}
removePassword() {
sed -i 's#<sheetProtection[^>]*>##' "$1"
}
removePasswords() {
grep -Rn sheetProtection "$TEMP_DIRECTORY" | cut -d: -f1 | while read -r file; do
removePassword "$file"
log "Password removed in: $file"
done
}
cleanUp() {
rm -fr "$TEMP_DIRECTORY" && log "Temp directory removed: $TEMP_DIRECTORY"
}
ERROR=0
VERBOSE=0
while getopts "hvi:o:" option; do
case "$option" in
i) INPUT_FILE="$OPTARG";;
o) OUTPUT_FILE="$OPTARG";;
h) usage; exit 0;;
v) VERBOSE=1;;
:) ERROR=1;;
?) ERROR=1;;
esac
done
if [ $ERROR -eq 1 ]; then
error "Invalid parameter\\n$(usage)"
fi
exit_if_empty "$INPUT_FILE" "Input file is missing (-i)"
exit_if_empty "$OUTPUT_FILE" "Output file is missing (-i)"
if [ ! -f "$INPUT_FILE" ]; then
error "No such file: $INPUT_FILE"
fi
createTmpDirectory
unzipExcel
removePasswords
createExcel
cleanUp