better erro handling
[small-misc-bash] / ex
1 #!/bin/bash
2 # Copyright (C) 2014 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 ex() {
17 local help="Usage: ex [--help|-q] FILE...
18 Extract many types of files
19
20 Based on their extensions,
21 7z [tar.]bz2 deb [tar.]gz iso dsc rar rpm sfs tar xpi [tar.]xz zip Z.
22 See source for exact file extensions.
23 Note: dtrx (package & command) extracts most of these plus some others."
24
25 local quiet=false
26 case $1 in
27 --help)
28 echo "$help"
29 return 0
30 ;;
31 -q)
32 quiet=true
33 shift
34 ;;
35 esac
36 local x super restore_nullglob f
37 for x in "$@"; do
38 case "$x" in
39 # tars first, so they are higher pri than non-tar extensions
40 *.tar )
41 $quiet || echo tar xf "$x"
42 tar xf "$x"
43 ;;
44 *.tar.bz2 | *.tbz2 )
45 $quiet || echo tar xjf "$x"
46 tar xjf "$x"
47 ;;
48 *.tar.gz | *.tgz )
49 $quiet || echo tar xzf "$x"
50 tar xzf "$x"
51 ;;
52 *.tar.xz )
53 $quiet || echo tar Jxf "$x"
54 tar Jxf "$x"
55 ;;
56 *.xz)
57 $quiet || echo pixz -d "$x"
58 pixz -d "$x"
59 ;;
60 *.7z )
61 $quiet || echo 7za x "$x"
62 7za x "$x"
63 ;;
64 *.bz2 )
65 $quiet || echo bunzip2 "$x"
66 bunzip2 "$x"
67 ;;
68 *.deb )
69 $quiet || ar x "$x"
70 ar x "$x"
71 restore_nullglob=false
72 if ! shopt -q nullglob; then
73 restore_nullglob=true
74 fi
75 tmp=(data.tar.* control.tar.*)
76 if $restore_nullglob; then
77 shopt -u nullglob
78 fi
79 if (( ${#tmp[@]} != 2 )); then
80 echo "error: could not find data.tar.* or control.tar.*" >&2
81 return 1
82 fi
83 for f in ${tmp[@]}; do
84 case $f in
85 *.xz)
86 $quiet || echo tar xJf $f
87 tar xJf $f
88 rm -f $f
89 ;;
90 *.gz)
91 $quiet || echo tar xzf $f
92 tar xzf $f
93 rm -f $f
94 ;;
95 *)
96 echo "ex: ERROR: file i didnt expect: $f"
97 ;;
98 esac
99 done
100 ;;
101 *.dsc)
102 $quiet || echo dpkg-source -x "$x"
103 dpkg-source -x "$x"
104 ;;
105 *.gz )
106 $quiet || echo gunzip "$x"
107 gunzip "$x"
108 ;;
109 *.iso )
110 local super
111 if [[ $EUID != 0 ]]; then
112 super=sudo
113 fi
114 local temp_dir
115 temp_dir=$(mktemp -d)
116 $super losetup -f
117 $quiet || echo $super mount -o loop "$x" "$temp_dir"
118 $super mount -o loop "$x" "$temp_dir"
119 local dir="${x%%.iso}"
120 mkdir "$dir"
121 $quiet || cp -a "$temp_dir"/* "$dir"
122 cp -a "$temp_dir"/* "$dir"
123 $quiet || $super umount "$temp_dir"
124 $super umount "$temp_dir"
125 ;;
126 *.jar)
127 $quiet || echo jar xf "$x"
128 jar xf "$x"
129 ;;
130 *.r[0-9][0-9]|*.rar )
131 $quiet || echo unrar x "$x"
132 unrar x "$x"
133 ;;
134 *.rpm )
135 $quiet || echo "rpm2cpio $x | cpio --extract --make-directories"
136 rpm2cpio "$x" | cpio --extract --make-directories
137 ;;
138 *.sfs)
139 $quiet || echo $super unsquashfs "$x"
140 $super unsquashfs "$x"
141 ;;
142 *.Z )
143 $quiet || echo uncompress "$x"
144 uncompress "$x"
145 ;;
146 *.zip|*.xpi )
147 $quiet || echo unzip "$x"
148 unzip "$x"
149 ;;
150
151 *) echo "I don't know how to extract $x";;
152 esac
153 done
154 }
155 ex "$@"