Bulk renamer shell script - Programming On Unix

Users browsing this thread: 1 Guest(s)
seninha
Long time nixers
(20-04-2020, 03:46 PM)jkl Wrote: Tested with Schily's latest bosh shell: Works just fine. :)

Thanks for testing! I'm really thankful of that.

(20-04-2020, 03:45 PM)z3bra Wrote: And btw, moreutils should be installed everywhere, be it only for "sponge(1)"…

Moreutils is indeed very useful. I use ifne(1) for a dmenu of manpages. It makes zathura not open if the input is empty.

Code:
man -k . | sort -t ' ' -k2,2 -k1,1 | dmenu -i -l 8 -p man: | sed -E 's/^([^        ]+) \(([^       ]+)\).*/\2 \1/' | xargs -r man -Tpdf | ifne zathura -

I had no idea of the existence of vidir(1), even though I have moreutils installed.
I only have moreutils installed for ifne, but now, after you have opened my eyes, I will check the other utilities of its.

For sponge(1), I have yet another shell script that do changes inplace:
Code:
#!/bin/sh
# inplace: make changes from file inplace

usage() {
    echo "usage: inplace file cmd [args...]" 1>&2
    exit 2
}

[ "$#" -le 1 ] && usage

file="$1"
shift

new=$(mktemp)
old=$(mktemp)

trap 'rm -f $new $old; exit 1' EXIT     # clean up files

if "$@" <"$file" >"$new"                # collect input
then
    cp "$file" "$old"                   # save original file
    trap '' EXIT                        # we are commited, ignore signals
    cp "$new" "$file"
else
    echo "inplace: $1 failed, $file unchanged" 1>&2
    exit 1
fi

rm -f $new $old

exit 0

So the following two one-liners are equivalent:
Code:
inplace file sed ...
sed ... <file >file.tmp && mv -f file.tmp file

inplace(1) is based on overwrite(1), a script I found on the book The UNIX Programming Environment.


Messages In This Thread
Bulk renamer shell script - by seninha - 19-04-2020, 12:06 PM
RE: Bulk renamer shell script - by jkl - 20-04-2020, 08:04 AM
RE: Bulk renamer shell script - by seninha - 20-04-2020, 09:26 AM
RE: Bulk renamer shell script - by z3bra - 20-04-2020, 10:21 AM
RE: Bulk renamer shell script - by seninha - 20-04-2020, 10:28 AM
RE: Bulk renamer shell script - by z3bra - 20-04-2020, 03:45 PM
RE: Bulk renamer shell script - by jkl - 20-04-2020, 03:46 PM
RE: Bulk renamer shell script - by seninha - 20-04-2020, 04:11 PM
RE: Bulk renamer shell script - by ckester - 20-04-2020, 05:49 PM