shellutils
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/fixname Thu Apr 23 13:09:47 2015 +0300 1.3 @@ -0,0 +1,143 @@ 1.4 +#!/bin/sh 1.5 +# ___________________________________________________ 1.6 +# / \_ 1.7 +# | fixname - cleans up filenames | \ 1.8 +# | | | 1.9 +# | author: John Tsiombikas <nuclear@member.fsf.org> | | 1.10 +# | license: public domain | | 1.11 +# \___________________________________________________/ | 1.12 +# \___________________________________________________/ 1.13 +# 1.14 + 1.15 +name=`basename $0` 1.16 +proc_args=true 1.17 +rename=false 1.18 +verbose=false 1.19 + 1.20 + 1.21 +print_usage() 1.22 +{ 1.23 + echo "Usage: $name [options] name1 name2 ... namen" 1.24 + echo 1.25 + echo 'Cleans up the names passed as arguments, and writes them out one per line' 1.26 + echo 'options:' 1.27 + echo ' - use stdin instead of arguments' 1.28 + echo ' -- stop processing options, the rest are considered names to be fixed' 1.29 + echo " -r rename it, don't just print the clean name" 1.30 + echo ' -v verbose output' 1.31 + echo ' -h print usage and exit' 1.32 + echo 'more options will be added in the future to control exactly what fixes are performed' 1.33 +} 1.34 + 1.35 +uscore() 1.36 +{ 1.37 + sed 's/ /_/g' 1.38 +} 1.39 + 1.40 +udashu() 1.41 +{ 1.42 + sed 's/_-_/-/g' 1.43 +} 1.44 + 1.45 +dupl() 1.46 +{ 1.47 + sed 's/__/_/g' | sed 's/--/-/g' 1.48 +} 1.49 + 1.50 +utrail() 1.51 +{ 1.52 + sed 's/_$//g' | sed 's/-$//g' 1.53 +} 1.54 + 1.55 +tolower() 1.56 +{ 1.57 + tr [:upper:] [:lower:] 1.58 +} 1.59 + 1.60 +brac() 1.61 +{ 1.62 + sed 's/[][)(}{><]//g' 1.63 +} 1.64 + 1.65 +diacrit() 1.66 +{ 1.67 + sed s/\'//g | sed 's/["`,;\\]//g' 1.68 +} 1.69 + 1.70 +symb() 1.71 +{ 1.72 + sed 's/[!?@#$%^&*=]//g' 1.73 +} 1.74 + 1.75 +transform() 1.76 +{ 1.77 + uscore | udashu | tolower | brac | diacrit | symb | dupl | utrail 1.78 +} 1.79 + 1.80 +fix() 1.81 +{ 1.82 + fixed=`echo $1 | transform` 1.83 + 1.84 + if [ "$1" = "$fixed" ]; then 1.85 + return 1.86 + fi 1.87 + 1.88 + if $rename; then 1.89 + if $verbose; then 1.90 + echo "$1 -> $fixed" 1.91 + fi 1.92 + 1.93 + mv "$1" "$fixed" 1.94 + else 1.95 + echo "$fixed" 1.96 + fi 1.97 +} 1.98 + 1.99 +fixed_any=false 1.100 + 1.101 +while [ $# -gt 0 ]; do 1.102 + if $proc_args; then 1.103 + 1.104 + case $1 in 1.105 + -) 1.106 + shift 1.107 + transform 1.108 + exit 0 1.109 + ;; 1.110 + 1.111 + --) 1.112 + proc_args=false 1.113 + ;; 1.114 + 1.115 + -r) 1.116 + rename=true 1.117 + ;; 1.118 + 1.119 + -v) 1.120 + verbose=true 1.121 + ;; 1.122 + 1.123 + -h) 1.124 + print_usage 1.125 + exit 0 1.126 + ;; 1.127 + 1.128 + *) 1.129 + fix "$1" 1.130 + fixed_any=true 1.131 + ;; 1.132 + esac 1.133 + else 1.134 + fix "$1" 1.135 + fixed_any=true 1.136 + fi 1.137 + shift 1.138 +done 1.139 + 1.140 +if ! $fixed_any; then 1.141 + for i in *; do 1.142 + if [ -f "$i" ]; then 1.143 + fix "$i" 1.144 + fi 1.145 + done 1.146 +fi