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