diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2017-04-05 20:06:39 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2017-04-05 20:46:47 +1200 |
commit | c8ab406749124d2e762ad5cf53963070113afd0f (patch) | |
tree | 54c9721a06957ebe7098a211eea803b0230c0f5d /bin/stex.sh | |
parent | Handle POSIX correctness in ~/.bash_profile (diff) | |
download | dotfiles-c8ab406749124d2e762ad5cf53963070113afd0f.tar.gz dotfiles-c8ab406749124d2e762ad5cf53963070113afd0f.zip |
Apply runtime shebanging to POSIX shell
Diffstat (limited to 'bin/stex.sh')
-rw-r--r-- | bin/stex.sh | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/bin/stex.sh b/bin/stex.sh new file mode 100644 index 00000000..14d2cabf --- /dev/null +++ b/bin/stex.sh @@ -0,0 +1,38 @@ +# 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}" |