Merge pull request 'Better help and error management' (#1) from dev into main

Reviewed-on: cedric/extract#1
This commit is contained in:
cedric 2025-06-23 13:45:47 +02:00
commit 3f6f547cb0

63
extract
View file

@ -1,19 +1,46 @@
#!/bin/bash
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted" ;;
esac
else
echo "'$1' invalid file"
fi
set -euo pipefail
IFS=$'\n\t'
usage() {
echo "Usage: $0 <archive-file>"
echo "Extract supported archive formats: .tar.gz, .zip, .rar, .7z, etc."
}
check_command() {
local cmd="$1"
if ! command -v "$cmd" &>/dev/null; then
echo "Error: required command '$cmd' not found."
exit 3
fi
}
if [[ $# -eq 0 || "$1" == "--help" ]]; then
usage
exit 0
fi
file="$1"
if [[ ! -f "$file" ]]; then
echo "Error: '$file' is not a valid file."
exit 2
fi
case "$file" in
*.tar.bz2) check_command tar; tar xjf "$file" ;;
*.tar.gz) check_command tar; tar xzf "$file" ;;
*.bz2) check_command bunzip2; bunzip2 "$file" ;;
*.rar) check_command unrar; unrar x "$file" ;;
*.gz) check_command gunzip; gunzip "$file" ;;
*.tar) check_command tar; tar xf "$file" ;;
*.tbz2) check_command tar; tar xjf "$file" ;;
*.tgz) check_command tar; tar xzf "$file" ;;
*.zip) check_command unzip; unzip "$file" ;;
*.Z) check_command uncompress; uncompress "$file" ;;
*.7z) check_command 7z; 7z x "$file" ;;
*)
echo "Error: '$file' has an unsupported file extension."
exit 1
;;
esac