aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorGravatar KlzXS <azszwymmvqdi@yahoo.com>2020-03-15 15:19:05 +0100
committerGravatar GitHub <noreply@github.com>2020-03-15 19:49:05 +0530
commitc215c5de8087e1cc352f4f7f0d187231ac367a8c (patch)
tree2b3ae5189b40a785f8b355f6579da48f403c6522 /plugins
parent67994ff4b2bb1a77ba987cf4ac0793ef13190c3a (diff)
downloadnnn-c215c5de8087e1cc352f4f7f0d187231ac367a8c.tar.gz
Convert batchrename to bash and rename it (#495)
Diffstat (limited to 'plugins')
-rwxr-xr-xplugins/.nmv (renamed from plugins/batchrename)103
1 files changed, 22 insertions, 81 deletions
diff --git a/plugins/batchrename b/plugins/.nmv
index b9b9a84..8b080ae 100755
--- a/plugins/batchrename
+++ b/plugins/.nmv
@@ -1,4 +1,4 @@
-#!/usr/bin/env sh
+#!/usr/bin/env bash
# Description: An almost fully POSIX compliant batch file renamer
#
@@ -11,7 +11,7 @@
# 4. Can remove files
# 5. Switch number pairs to swap filenames
#
-# Shell: POSIX compliant
+# Shell: bash
# Author: KlzXS
EDITOR="${EDITOR:-vi}"
@@ -39,9 +39,12 @@ else
fi
printf "%s" "$arr" | awk '{print NR " " $0}' > "$dst_file"
-arr=$(printf "%s" "$arr" | sed "s/'/'\\\\''/g;s/^\(.*\)$/'\1'/" | tr '\n' ' ')
-eval "set -- $arr"
+items=("~")
+while IFS='' read -r line; do
+ items+=("$line");
+done < <(printf "%s\n" "$arr")
+
$EDITOR "$dst_file"
while read -r num name; do
@@ -60,7 +63,7 @@ while read -r num name; do
exit 1
fi
- src=$(eval printf "%s" "\"\${$num}\"")
+ src=${items[$num]}
if [ -z "$src" ]; then
printf "%s: unknown item number %s\n" "$0" "$num" > /dev/stderr
@@ -73,24 +76,7 @@ while read -r num name; do
if [ ! -e "$src" ] && [ ! -L "$src" ]; then
printf "%s: %s does not exit\n" "$0" "$src" > /dev/stderr
- c=1
- new_args=""
- while [ $c -le $# ]; do
- tmp=$(eval printf "%s" "\"\${$c}\"")
- if [ "$c" -eq "$num" ]; then
- new_args="$new_args ''"
- else
- if [ -z "$tmp" ]; then
- tmp="''"
- else
- tmp=$(printf "%s" "$tmp" | sed "s/'/'\\\\''/g;s/^\(.*\)$/'\1'/")
- fi
- new_args="$new_args $tmp"
- fi
- c=$((c+1))
- done
-
- eval "set -- $new_args"
+ unset "items[$num]"
continue
fi
@@ -110,26 +96,12 @@ while read -r num name; do
printf "%s: failed to rename %s to %s: %s\n" "$0" "$name" "$tmp" "$!" > /dev/stderr
exit_status=1
fi
-
- c=1
- new_args=""
- while [ $c -le $# ]; do
- item=$(eval printf "%s" "\"\${$c}\"")
- if [ "$item" = "$name" ]; then
- item=$(printf "%s" "$tmp" | sed "s/'/'\\\\''/g;s/^\(.*\)$/'\1'/")
- new_args="$new_args $item"
- else
- if [ -z "$item" ]; then
- item="''"
- else
- item=$(printf "%s" "$item" | sed "s/'/'\\\\''/g;s/^\(.*\)$/'\1'/")
- fi
- new_args="$new_args $item"
+
+ for key in "${!items[@]}"; do
+ if [ "${items[$key]}" = "$name" ]; then
+ items[$key]="$tmp"
fi
- c=$((c+1))
done
-
- eval "set -- $new_args"
fi
dir=$(dirname "$name")
@@ -140,55 +112,24 @@ while read -r num name; do
printf "%s: failed to rename %s to %s: %s\n" "$0" "$name" "$tmp" "$!" > /dev/stderr
exit_status=1
else
- printf "'%s' -> '%s'\n" "$src" "$name"
if [ -d "$name" ]; then
- c=1
- new_args=""
- while [ $c -le $# ]; do
- tmp=$(eval printf "\"\${$c}\"")
- if [ -z "$tmp" ]; then
- tmp="''"
- else
- tmp=$(printf "%s" "$tmp" | sed "s|^$src\(\$\|\/\)|$name\1|;s/'/'\\\\''/g;s/^\(.*\)$/'\1'/")
- fi
- new_args="$new_args $tmp"
- c=$((c+1))
+ for key in "${!items[@]}"; do
+ items[$key]=$(printf "%s" "${items[$key]}" | sed "s|^$src\(\$\|\/\)|$name\1|")
done
-
- eval "set -- $new_args"
+
printf "'%s' => '%s'\n" "$src" "$name"
- fi
- fi
- fi
-
- c=1
- new_args=""
- while [ $c -le $# ]; do
- tmp=$(eval printf "%s" "\"\${$c}\"")
- if [ "$c" -eq "$num" ]; then
- new_args="$new_args ''"
- else
- if [ -z "$tmp" ]; then
- tmp="''"
else
- tmp=$(printf "%s" "$tmp" | sed "s/'/'\\\\''/g;s/^\(.*\)$/'\1'/")
+ printf "'%s' -> '%s'\n" "$src" "$name"
fi
- new_args="$new_args $tmp"
fi
- c=$((c+1))
- done
+ fi
- eval "set -- $new_args"
+ unset "items[$num]"
done <"$dst_file"
-c=1
-new_args=""
-while [ $c -le $# ]; do
- tmp=$(eval printf "%s" "\"\${$c}\"")
- if [ -n "$tmp" ]; then
- rm -ri "$tmp"
- fi
- c=$((c+1))
+unset "items[0]"
+for item in "${items[@]}"; do
+ rm -ri "$item"
done
rm "$dst_file"