Browse Source

metaflac-field2field: minor improvements, shellcheck

Matteo Cypriani 9 months ago
parent
commit
aca5e08738
1 changed files with 39 additions and 35 deletions
  1. 39
    35
      audio/metaflac-field2field.sh

+ 39
- 35
audio/metaflac-field2field.sh View File

@@ -18,41 +18,45 @@
18 18
 
19 19
 #set -x
20 20
 
21
-print_usage()
22
-{
23
-    echo "Usage:"
24
-    echo "  $0 <ACTION> <SRC_FIELD> <DST_FIELD> <FILE.flac> [ <FILE2.flac> [...] ]"
25
-    echo "<ACTION> : copy | move | rmcopy | mvcopy"
26
-    echo "  copy: each occurrence of SRC_FIELD is duplicated to create an occurrence of"
27
-    echo "        DST_FIELD; all the existing occurrences of SRC_FIELD and DST_FIELD are"
28
-    echo "        kept."
29
-    echo "  move: each occurrence of SRC_FIELD is renamed to create an occurrence of"
30
-    echo "        DST_FIELD; the existing occurrences of DST_FIELD are kept."
31
-    echo "  rmcopy: the existing occurrences of DST_FIELD are deleted, then each"
32
-    echo "          occurrence of SRC_FIELD is duplicated to create an occurrence"
33
-    echo "          of DST_FIELD; the occurrences of SRC_FIELD are kept."
34
-    echo "  rmmove: the existing occurrences of DST_FIELD are deleted, then each"
35
-    echo "          occurrence of SRC_FIELD is renamed to create an occurrence of"
36
-    echo "          DST_FIELD."
37
-    exit 1
38
-}
21
+USAGE=$(cat <<EOF
22
+Usage:
23
+  $0 <ACTION> <SRC_FIELD> <DST_FIELD> <FILE.flac> [ <FILE2.flac> [...] ]
24
+
25
+<ACTION> : copy | move | rmcopy | mvcopy
26
+  copy: each occurrence of SRC_FIELD is duplicated to create an occurrence of
27
+        DST_FIELD; all the existing occurrences of SRC_FIELD and DST_FIELD are
28
+        kept.
29
+  move: each occurrence of SRC_FIELD is renamed to create an occurrence of
30
+        DST_FIELD; the existing occurrences of DST_FIELD are kept.
31
+  rmcopy: the existing occurrences of DST_FIELD are deleted, then each
32
+          occurrence of SRC_FIELD is duplicated to create an occurrence
33
+          of DST_FIELD; the occurrences of SRC_FIELD are kept.
34
+  rmmove: the existing occurrences of DST_FIELD are deleted, then each
35
+          occurrence of SRC_FIELD is renamed to create an occurrence of
36
+          DST_FIELD.
37
+EOF
38
+)
39 39
 
40 40
 # Right number of arguments?
41 41
 if [ $# -lt 4 ] ; then
42
-    echo "Wrong number of arguments!"
42
+    echo "Wrong number of arguments!" >&2
43 43
     print_usage
44 44
 fi
45 45
 
46
-ACTION=$1
46
+ACTION="$1"
47 47
 shift
48 48
 
49 49
 # Known action type?
50
-case $ACTION in
50
+case "$ACTION" in
51 51
     copy) ;;
52 52
     move) ;;
53 53
     rmcopy) ;;
54 54
     rmmove) ;;
55
-    *) echo "Action unknown \"$ACTION\"!" ; print_usage ;;
55
+    *)
56
+        printf 'Action unknown "%s"!\n\n' "$ACTION" >&2
57
+        printf '%s\n' "$USAGE" >&2
58
+        exit 1
59
+        ;;
56 60
 esac
57 61
 
58 62
 SRC_FIELD=$1
@@ -60,28 +64,28 @@ shift
60 64
 DST_FIELD=$1
61 65
 shift
62 66
 
63
-echo "Source field: $SRC_FIELD"
64
-echo "Destination field: $DST_FIELD"
65
-echo "Action: $ACTION"
67
+printf 'Source field: %s\n' "$SRC_FIELD"
68
+printf 'Destination field: %s\n' "$DST_FIELD"
69
+printf 'Action: %s\n' "$ACTION"
66 70
 
67 71
 echo "Processing files..."
68 72
 
69
-if echo $ACTION | egrep '^rm' >/dev/null ; then
70
-    echo "Deleting field $DST_FIELD on each file..."
71
-    metaflac --remove-tag=$DST_FIELD "$@"
73
+if echo "$ACTION" | grep '^rm' >/dev/null ; then
74
+    printf 'Deleting field "%s" on each file...\n' "$DST_FIELD"
75
+    metaflac --remove-tag="$DST_FIELD" "$@"
72 76
 fi
73 77
 
74 78
 for file in "$@" ; do
75
-    echo "Setting field \"$DST_FIELD\" on file: $file"
76
-    metaflac --show-tag=$SRC_FIELD "$file" | sed 's/^[A-Z]\+=//' | while read c1 ; do
77
-    echo "Adding $DST_FIELD=$c1..."
78
-    metaflac --set-tag=$DST_FIELD="$c1" "$file"
79
+    printf 'Setting field "%s" on file: "%s"\n' "$DST_FIELD" "$file"
80
+    metaflac --show-tag="$SRC_FIELD" "$file" | sed 's/^[A-Z]\+=//' | while read -r c1 ; do
81
+    printf 'Adding "%s"...\n' "$DST_FIELD=$c1"
82
+    metaflac --set-tag="$DST_FIELD"="$c1" "$file"
79 83
 done
80 84
 done
81 85
 
82
-if echo $ACTION | egrep 'move$' >/dev/null ; then
83
-    echo "Deleting $SRC_FIELD on each file..."
84
-    metaflac --remove-tag=$SRC_FIELD "$@"
86
+if echo "$ACTION" | grep 'move$' >/dev/null ; then
87
+    printf 'Deleting "%s" on each file...\n' "$SRC_FIELD"
88
+    metaflac --remove-tag="$SRC_FIELD" "$@"
85 89
 fi
86 90
 
87 91
 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

Loading…
Cancel
Save