shellutils

annotate fixname @ 0:80fc0c4b11c0

collection of shell scripts
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Apr 2015 13:09:47 +0300
parents
children
rev   line source
nuclear@0 1 #!/bin/sh
nuclear@0 2 # ___________________________________________________
nuclear@0 3 # / \_
nuclear@0 4 # | fixname - cleans up filenames | \
nuclear@0 5 # | | |
nuclear@0 6 # | author: John Tsiombikas <nuclear@member.fsf.org> | |
nuclear@0 7 # | license: public domain | |
nuclear@0 8 # \___________________________________________________/ |
nuclear@0 9 # \___________________________________________________/
nuclear@0 10 #
nuclear@0 11
nuclear@0 12 name=`basename $0`
nuclear@0 13 proc_args=true
nuclear@0 14 rename=false
nuclear@0 15 verbose=false
nuclear@0 16
nuclear@0 17
nuclear@0 18 print_usage()
nuclear@0 19 {
nuclear@0 20 echo "Usage: $name [options] name1 name2 ... namen"
nuclear@0 21 echo
nuclear@0 22 echo 'Cleans up the names passed as arguments, and writes them out one per line'
nuclear@0 23 echo 'options:'
nuclear@0 24 echo ' - use stdin instead of arguments'
nuclear@0 25 echo ' -- stop processing options, the rest are considered names to be fixed'
nuclear@0 26 echo " -r rename it, don't just print the clean name"
nuclear@0 27 echo ' -v verbose output'
nuclear@0 28 echo ' -h print usage and exit'
nuclear@0 29 echo 'more options will be added in the future to control exactly what fixes are performed'
nuclear@0 30 }
nuclear@0 31
nuclear@0 32 uscore()
nuclear@0 33 {
nuclear@0 34 sed 's/ /_/g'
nuclear@0 35 }
nuclear@0 36
nuclear@0 37 udashu()
nuclear@0 38 {
nuclear@0 39 sed 's/_-_/-/g'
nuclear@0 40 }
nuclear@0 41
nuclear@0 42 dupl()
nuclear@0 43 {
nuclear@0 44 sed 's/__/_/g' | sed 's/--/-/g'
nuclear@0 45 }
nuclear@0 46
nuclear@0 47 utrail()
nuclear@0 48 {
nuclear@0 49 sed 's/_$//g' | sed 's/-$//g'
nuclear@0 50 }
nuclear@0 51
nuclear@0 52 tolower()
nuclear@0 53 {
nuclear@0 54 tr [:upper:] [:lower:]
nuclear@0 55 }
nuclear@0 56
nuclear@0 57 brac()
nuclear@0 58 {
nuclear@0 59 sed 's/[][)(}{><]//g'
nuclear@0 60 }
nuclear@0 61
nuclear@0 62 diacrit()
nuclear@0 63 {
nuclear@0 64 sed s/\'//g | sed 's/["`,;\\]//g'
nuclear@0 65 }
nuclear@0 66
nuclear@0 67 symb()
nuclear@0 68 {
nuclear@0 69 sed 's/[!?@#$%^&*=]//g'
nuclear@0 70 }
nuclear@0 71
nuclear@0 72 transform()
nuclear@0 73 {
nuclear@0 74 uscore | udashu | tolower | brac | diacrit | symb | dupl | utrail
nuclear@0 75 }
nuclear@0 76
nuclear@0 77 fix()
nuclear@0 78 {
nuclear@0 79 fixed=`echo $1 | transform`
nuclear@0 80
nuclear@0 81 if [ "$1" = "$fixed" ]; then
nuclear@0 82 return
nuclear@0 83 fi
nuclear@0 84
nuclear@0 85 if $rename; then
nuclear@0 86 if $verbose; then
nuclear@0 87 echo "$1 -> $fixed"
nuclear@0 88 fi
nuclear@0 89
nuclear@0 90 mv "$1" "$fixed"
nuclear@0 91 else
nuclear@0 92 echo "$fixed"
nuclear@0 93 fi
nuclear@0 94 }
nuclear@0 95
nuclear@0 96 fixed_any=false
nuclear@0 97
nuclear@0 98 while [ $# -gt 0 ]; do
nuclear@0 99 if $proc_args; then
nuclear@0 100
nuclear@0 101 case $1 in
nuclear@0 102 -)
nuclear@0 103 shift
nuclear@0 104 transform
nuclear@0 105 exit 0
nuclear@0 106 ;;
nuclear@0 107
nuclear@0 108 --)
nuclear@0 109 proc_args=false
nuclear@0 110 ;;
nuclear@0 111
nuclear@0 112 -r)
nuclear@0 113 rename=true
nuclear@0 114 ;;
nuclear@0 115
nuclear@0 116 -v)
nuclear@0 117 verbose=true
nuclear@0 118 ;;
nuclear@0 119
nuclear@0 120 -h)
nuclear@0 121 print_usage
nuclear@0 122 exit 0
nuclear@0 123 ;;
nuclear@0 124
nuclear@0 125 *)
nuclear@0 126 fix "$1"
nuclear@0 127 fixed_any=true
nuclear@0 128 ;;
nuclear@0 129 esac
nuclear@0 130 else
nuclear@0 131 fix "$1"
nuclear@0 132 fixed_any=true
nuclear@0 133 fi
nuclear@0 134 shift
nuclear@0 135 done
nuclear@0 136
nuclear@0 137 if ! $fixed_any; then
nuclear@0 138 for i in *; do
nuclear@0 139 if [ -f "$i" ]; then
nuclear@0 140 fix "$i"
nuclear@0 141 fi
nuclear@0 142 done
nuclear@0 143 fi