From c8ab406749124d2e762ad5cf53963070113afd0f Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 5 Apr 2017 20:06:39 +1200 Subject: Apply runtime shebanging to POSIX shell --- bin/sqs.sh | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 bin/sqs.sh (limited to 'bin/sqs.sh') diff --git a/bin/sqs.sh b/bin/sqs.sh new file mode 100644 index 00000000..e00797e3 --- /dev/null +++ b/bin/sqs.sh @@ -0,0 +1,33 @@ +# Chop a trailing query string off filenames +self=sqs + +# Check args +if [ "$#" -eq 0 ] ; then + printf >&2 '%s: Need a filename\n' "$self" + exit 2 +fi + +# Iterate through the given files +for sn ; do + + # Strip trailing slash if any and then query string + sn=${sn%/} + dn=${sn%%\?*} + + # 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}" -- cgit v1.2.3