minor fixes and indenting
[small-misc-bash] / ex
diff --git a/ex b/ex
index cf04ee6f2f26e93d3ac5ec30b5030a39e7b55611..7859db3276e75e76486c8f17064e72d365785d39 100755 (executable)
--- a/ex
+++ b/ex
@@ -14,7 +14,7 @@
 # limitations under the License.
 
 ex() {
-    local help="Usage: ex [--help] FILE...
+  local help="Usage: ex [--help|-q] FILE...
 Extract many types of files
 
 Based on their extensions,
@@ -22,56 +22,130 @@ Based on their extensions,
 See source for exact file extensions.
 Note: dtrx (package & command) extracts most of these plus some others."
 
-    if [[ $1 == --help ]]; then
-        echo "$help"
-    fi
-    local x super
-    for x in "$@"; do
-        case "$x" in
-            # tars first, so they are higher pri than non-tar extensions
-            *.tar ) tar xf "$x" ;;
-            *.tar.bz2 | *.tbz2 ) tar xjf "$x" ;;
-            *.tar.gz | *.tgz ) tar xzf "$x" ;;
-            *.tar.xz ) tar Jxf "$x" ;;
-            *.xz) xz -d "$x" ;;
-            *.7z ) 7za x "$x" ;;
-            *.bz2 ) bunzip2 "$x" ;;
-            *.deb )
-              ar x "$x"
-              tmp=(data.tar.*)
-              case $tmp in
-                *.xz) tar xJf data.tar.xz ;;
-                *.gz) tar xzf data.tar.gz ;;
-                *) echo "error: could not find data.tar.*" >&2; exit 1 ;;
-              esac
-              tar xzf control.tar.gz
-              rm -f control.tar.gz
-              rm -f data.tar.*
+  local quiet=false
+  case $1 in
+    --help)
+      echo "$help"
+      return 0
+      ;;
+    -q)
+      quiet=true
+      shift
+      ;;
+  esac
+  local x super restore_nullglob f
+  for x in "$@"; do
+    case "$x" in
+      # tars first, so they are higher pri than non-tar extensions
+      *.tar )
+        $quiet || echo tar xf "$x"
+        tar xf "$x"
+        ;;
+      *.tar.bz2 | *.tbz2 )
+        $quiet || echo tar xjf "$x"
+        tar xjf "$x"
+        ;;
+      *.tar.gz | *.tgz )
+        $quiet || echo tar xzf "$x"
+        tar xzf "$x"
+        ;;
+      *.tar.xz )
+        $quiet || echo tar Jxf "$x"
+        tar Jxf "$x"
+        ;;
+      *.xz)
+        $quiet || echo xz -d "$x"
+        xz -d "$x"
+        ;;
+      *.7z )
+        $quiet || echo 7za x "$x"
+        7za x "$x"
+        ;;
+      *.bz2 )
+        $quiet || echo bunzip2 "$x"
+        bunzip2 "$x"
+        ;;
+      *.deb )
+        $quiet || ar x "$x"
+        ar x "$x"
+        restore_nullglob=false
+        if ! shopt -q nullglob; then
+          restore_nullglob=true
+        fi
+        tmp=(data.tar.* control.tar.*)
+        if $restore_nullglob; then
+          shopt -u nullglob
+        fi
+        if (( ${#tmp[@]} != 2 )); then
+          echo "error: could not find data.tar.* or control.tar.*" >&2
+          return 1
+        fi
+        for f in ${tmp[@]}; do
+          case $f in
+            *.xz)
+              $quiet || echo tar xJf $f
+              tar xJf $f
               ;;
-            *.dsc) dpkg-source -x "$x" ;;
-            *.gz ) gunzip  "$x" ;;
-            *.iso )
-                local super
-                if [[ $EUID != 0 ]]; then
-                    super=sudo
-                fi
-                local temp_dir=$(mktemp -d)
-                $super losetup -f
-                $super mount -o loop "$x" "$temp_dir"
-                local dir="${x%%.iso}"
-                mkdir "$dir"
-                cp -a "$temp_dir"/* "$dir"
-                $super umount "$temp_dir"
-                ;;
-            *.jar) jar xf "$x" ;;
-            *.r[0-9][0-9]|*.rar ) unrar x "$x" ;;
-            *.rpm ) rpm2cpio "$x" | cpio --extract --make-directories ;;
-            *.sfs) $super unsquashfs "$x" ;;
-            *.Z ) uncompress "$x" ;;
-            *.zip|*.xpi ) unzip "$x" ;;
+            *.gz)
+              $quiet || echo tar xzf $f
+              tar xzf $f
+              ;;
+          esac
+          rm -f $f
+        done
+        ;;
+      *.dsc)
+        $quiet || echo dpkg-source -x "$x"
+        dpkg-source -x "$x"
+        ;;
+      *.gz )
+        $quiet || echo gunzip "$x"
+        gunzip "$x"
+        ;;
+      *.iso )
+        local super
+        if [[ $EUID != 0 ]]; then
+          super=sudo
+        fi
+        local temp_dir
+        temp_dir=$(mktemp -d)
+        $super losetup -f
+        $quiet || echo $super mount -o loop "$x" "$temp_dir"
+        $super mount -o loop "$x" "$temp_dir"
+        local dir="${x%%.iso}"
+        mkdir "$dir"
+        $quiet || cp -a "$temp_dir"/* "$dir"
+        cp -a "$temp_dir"/* "$dir"
+        $quiet || $super umount "$temp_dir"
+        $super umount "$temp_dir"
+        ;;
+      *.jar)
+        $quiet || echo jar xf "$x"
+        jar xf "$x"
+        ;;
+      *.r[0-9][0-9]|*.rar )
+        $quiet || echo unrar x "$x"
+        unrar x "$x"
+        ;;
+      *.rpm )
+        $quiet || echo "rpm2cpio $x | cpio --extract --make-directories"
+        rpm2cpio "$x" | cpio --extract --make-directories
+        ;;
+      *.sfs)
+        $quiet || echo $super unsquashfs "$x"
+        $super unsquashfs "$x"
+        ;;
+      *.Z )
+        $quiet || echo uncompress "$x"
+        uncompress "$x"
+        ;;
+      *.zip|*.xpi )
+        $quiet || echo unzip "$x"
+        unzip "$x"
+        ;;
 
-            *)  echo "I don't know how to extract $x";;
-        esac
-    done
+      *)  echo "I don't know how to extract $x";;
+    esac
+  done
 }
 ex "$@"