Let’s break down what this script does in detail. The script is named ex.sh, authored by GJS (homelab-alpha), and its purpose is to provide a convenient way to extract various archive formats. It simplifies the extraction process by automatically detecting the file format and applying the appropriate extraction method.

Here’s a detailed explanation:

Script Metadata

  • Filename: ex.sh
  • Author: GJS (homelab-alpha)
  • Date: May 26, 2024
  • Version: 1.0.1
  • Description: This script provides a convenient way to extract various archive formats including tar, zip, gzip, bzip2, rar, etc. It simplifies the extraction process by automatically detecting the file format and applying the appropriate extraction method.
  • RAW Script: ex.sh

Function Declaration

# Function: ex
function ex() {

The script defines a function called ex. This function is responsible for handling the extraction of different archive formats.


File Existence Check

  # Check if the input file exists
  if [ -f "$1" ]; then

The function starts by checking if the input file (passed as the first argument $1) exists using the -f flag.


Case Statement for File Type Detection

    # Determine the file type and perform extraction accordingly
    case "$1" in

The script uses a case statement to determine the file type based on the file extension of the input file.


Handling Different File Types

Each case within the case statement handles a specific file extension:

  • .tar.bz2: Extracts using tar -jxvf

      *.tar.bz2) tar -jxvf "$1" ;;
    

  • .tar.gz: Extracts using tar -zxvf

      *.tar.gz) tar -zxvf "$1" ;;
    

  • .bz2: Extracts using bunzip2

      *.bz2) bunzip2 "$1" ;;
    

  • .dmg: Mounts the disk image using hdiutil

      *.dmg) hdiutil mount "$1" ;;
    

  • .gz: Extracts using gunzip

      *.gz) gunzip "$1" ;;
    

  • .tar: Extracts using tar -xvf

      *.tar) tar -xvf "$1" ;;
    

  • .tbz2: Extracts using tar -jxvf

      *.tbz2) tar -jxvf "$1" ;;
    

  • .tgz: Extracts using tar -zxvf

      *.tgz) tar -zxvf "$1" ;;
    

  • .zip or .ZIP: Extracts using unzip

      *.zip | *.ZIP) unzip "$1" ;;
    

  • .pax: Extracts using pax

      *.pax) pax -r <"$1" ;;
    

  • .pax.Z: Uncompresses and extracts using uncompress piped to pax

      *.pax.Z) uncompress "$1" --stdout | pax -r ;;
    

  • .rar: Extracts using unrar

      *.rar) unrar x "$1" ;;
    

  • .Z: Extracts using uncompress

      *.Z) uncompress "$1" ;;
    

Unsupported File Format

    *) echo "$1 cannot be extracted/mounted via ex()" ;; # Unsupported file format

If the file extension does not match any of the supported formats, the script prints a message indicating that the file cannot be extracted.


Non-Existent File Handling

  else
    echo "$1 is not a valid file" # Print error message for non-existent files
  fi

If the input file does not exist, the script prints an error message indicating that the file is not valid.


Conclusion

This script effectively handles the extraction of various archive formats, making it a handy tool for simplifying the process of dealing with compressed files.

Last updated 22 Sep 2024, 12:15 CEST . history