-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7zwrap
executable file
·74 lines (64 loc) · 1.63 KB
/
7zwrap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# examples
# 7zwrap -y e *.zip
command -v "7z" >/dev/null || die "7z not in PATH; run 'apt install p7zip-full'";
# Function to print usage
usage() {
echo "Usage: $0 <7z_command> [7z_options] <archive_file1> [archive_file2] ..."
echo "Example: $0 x -j foo.rar bar.zip"
exit 1
}
# Check if at least two arguments are provided (7z command and at least one file)
if [ $# -lt 2 ]; then
usage
fi
# Initialize arrays
options=()
files=()
command=$1
shift
# Parse arguments
for arg in "$@"; do
if [ ${#files[@]} -eq 0 ]; then
if [ -f "$arg" ]; then
files+=("$arg")
else
options+=("$arg")
fi
else
if [ -f "$arg" ]; then
files+=("$arg")
elif [ -d "$arg" ]; then
echo "Warning: Directory '$arg' ignored." >&2
else
echo "Error: '$arg' is not a valid file." >&2
exit 1
fi
fi
done
# Check if we have any files to process
if [ ${#files[@]} -eq 0 ]; then
echo "Error: No valid archive files specified."
usage
fi
# Function to process a single file
process_file() {
local file="$1"
local out_file="${file}.out"
local err_file="${file}.err"
echo "Processing: $file"
if 7z "$command" "${options[@]}" "$file" > "$out_file" 2> "$err_file"; then
echo "Successfully processed $file"
rm "$out_file" "$err_file"
# move processed files to a '_consumed' directory
mkdir -p _consumed
mv "$file" _consumed/
else
echo "Error processing $file" >&2
fi
echo
}
# Process each file
for file in "${files[@]}"; do
process_file "$file"
done