aboutsummaryrefslogtreecommitdiff
path: root/bin/stex
blob: f1f9c02972937623b095ac1a48a139e7f44e8d60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/sh
# Strip an extension from the given filenames
self=stex

# Check args
if [ "$#" -lt 2 ] ; then
    printf >&2 '%s: Need an extension .ext and a filename\n' \
        "$self"
    exit 2
fi

# Extension is first arg, shift it off
ext=$1
shift

# Iterate through the given files (remaining args)
for sn ; do

    # Strip trailing slash if any and then extension
    sn=${sn%/}
    dn=${sn%"$ext"}

    # Ignore this file if its name wouldn't change
    [ "$sn" != "$dn" ] || continue

    # Ignore this file if its name already exists (don't overwrite)
    if [ -e "$dn" ] ; then
        printf >&2 '%s: File named %s already exists\n' \
            "$self" "$dn"
        ex=1
        continue
    fi

    # Attempt a rename, flag an error if there was one
    mv -- "$sn" "$dn" || ex=1
done

# Exit with 1 if there was any failed mv(1) run
exit "${ex:-0}"