Browse Source

mvparent: rename variables, shellcheck

master
Matteo Cypriani 1 year ago
parent
commit
cc66a46cc1
1 changed files with 20 additions and 16 deletions
  1. 20
    16
      file_utils/mvparent.sh

+ 20
- 16
file_utils/mvparent.sh View File

@@ -8,43 +8,47 @@
8 8
 # To Public License, Version 2, as published by Sam Hocevar. See
9 9
 # http://sam.zoy.org/wtfpl/COPYING for more details.
10 10
 #
11
-# Moves the contents of the given directory into its parent directory.
11
+# Moves the contents of the given directory(ies) into its parent directory,
12
+# then removes the directory(ies).
13
+
14
+set -e
15
+#set -x
12 16
 
13 17
 if [ $# -lt 1 ] ; then
14
-    echo "Usage: `basename $0` DIRECTORY [DIRECTORY [...]]" >&2
18
+    echo "Usage: $(basename "$0") DIRECTORY [DIRECTORY [...]]" >&2
15 19
     exit 1
16 20
 fi
17 21
 
18
-while [ "$1" != "" ] ; do
19
-    REP="$1"
22
+while [ -n "$1" ] ; do
23
+    dir="$1"
20 24
     shift
21 25
 
22
-    if [ ! -d "$REP" ] ; then
23
-        echo "Warning! \"$REP\" is not a directory: ignoring." >&2
26
+    if [ ! -d "$dir" ] ; then
27
+        echo "Warning! \"$dir\" is not a directory: ignoring." >&2
24 28
         continue
25 29
     fi
26 30
 
27
-    TARGET=$(basename "$REP")
28
-    REP=$dirname "$REP")
31
+    target="$(basename "$dir")"
32
+    dir="$(dirname "$dir")"
29 33
 
30
-    if [ "$TARGET" = "." -o "$TARGET" = ".." ] ; then
34
+    if [ "$target" = "." ] || [ "$target" = ".." ] ; then
31 35
         echo "Target cannot end by « . » or « .. »!" >&2
32 36
         exit 2
33 37
     fi
34 38
 
35
-    echo "Moving the contents of « $REP/$TARGET » into « $REP »..." >&2
39
+    echo "Moving the contents of « $dir/$target » into « $dir »..." >&2
36 40
 
37
-    cd "$REP"
41
+    cd "$dir"
38 42
 
39
-    if [ "$(echo "$TARGET"/*)" != "$TARGET/*" ] ; then
40
-        mv -n "$TARGET"/* .
43
+    if [ "$(echo "$target"/*)" != "$target/*" ] ; then
44
+        mv -n "$target"/* .
41 45
     fi
42 46
 
43
-    if [ "$(echo "$TARGET"/.[!.]*)" != "$TARGET/.[!.]*" ] ; then
44
-        mv -n "$TARGET"/.[!.]* .
47
+    if [ "$(echo "$target"/.[!.]*)" != "$target/.[!.]*" ] ; then
48
+        mv -n "$target"/.[!.]* .
45 49
     fi
46 50
 
47
-    rmdir "$TARGET"
51
+    rmdir "$target"
48 52
 done
49 53
 
50 54
 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

Loading…
Cancel
Save