Convert batchrename to bash and rename it (#495)

This commit is contained in:
KlzXS 2020-03-15 15:19:05 +01:00 committed by GitHub
parent 67994ff4b2
commit c215c5de80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,4 @@
#!/usr/bin/env sh #!/usr/bin/env bash
# Description: An almost fully POSIX compliant batch file renamer # Description: An almost fully POSIX compliant batch file renamer
# #
@ -11,7 +11,7 @@
# 4. Can remove files # 4. Can remove files
# 5. Switch number pairs to swap filenames # 5. Switch number pairs to swap filenames
# #
# Shell: POSIX compliant # Shell: bash
# Author: KlzXS # Author: KlzXS
EDITOR="${EDITOR:-vi}" EDITOR="${EDITOR:-vi}"
@ -39,9 +39,12 @@ else
fi fi
printf "%s" "$arr" | awk '{print NR " " $0}' > "$dst_file" 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" $EDITOR "$dst_file"
while read -r num name; do while read -r num name; do
@ -60,7 +63,7 @@ while read -r num name; do
exit 1 exit 1
fi fi
src=$(eval printf "%s" "\"\${$num}\"") src=${items[$num]}
if [ -z "$src" ]; then if [ -z "$src" ]; then
printf "%s: unknown item number %s\n" "$0" "$num" > /dev/stderr 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 if [ ! -e "$src" ] && [ ! -L "$src" ]; then
printf "%s: %s does not exit\n" "$0" "$src" > /dev/stderr printf "%s: %s does not exit\n" "$0" "$src" > /dev/stderr
c=1 unset "items[$num]"
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"
continue continue
fi 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 printf "%s: failed to rename %s to %s: %s\n" "$0" "$name" "$tmp" "$!" > /dev/stderr
exit_status=1 exit_status=1
fi fi
c=1 for key in "${!items[@]}"; do
new_args="" if [ "${items[$key]}" = "$name" ]; then
while [ $c -le $# ]; do items[$key]="$tmp"
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"
fi fi
c=$((c+1))
done done
eval "set -- $new_args"
fi fi
dir=$(dirname "$name") 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 printf "%s: failed to rename %s to %s: %s\n" "$0" "$name" "$tmp" "$!" > /dev/stderr
exit_status=1 exit_status=1
else else
printf "'%s' -> '%s'\n" "$src" "$name"
if [ -d "$name" ]; then if [ -d "$name" ]; then
c=1 for key in "${!items[@]}"; do
new_args="" items[$key]=$(printf "%s" "${items[$key]}" | sed "s|^$src\(\$\|\/\)|$name\1|")
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))
done done
eval "set -- $new_args"
printf "'%s' => '%s'\n" "$src" "$name" printf "'%s' => '%s'\n" "$src" "$name"
else
printf "'%s' -> '%s'\n" "$src" "$name"
fi fi
fi fi
fi fi
c=1 unset "items[$num]"
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"
done <"$dst_file" done <"$dst_file"
c=1 unset "items[0]"
new_args="" for item in "${items[@]}"; do
while [ $c -le $# ]; do rm -ri "$item"
tmp=$(eval printf "%s" "\"\${$c}\"")
if [ -n "$tmp" ]; then
rm -ri "$tmp"
fi
c=$((c+1))
done done
rm "$dst_file" rm "$dst_file"