-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace
executable file
·141 lines (123 loc) · 2.28 KB
/
replace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash -
usage() {
cat <<EOF
Usage:
$(basename -- "$0") [options] <search> <replace> <file> ...
Rename the <file>(s) by replacing <search> (glob pattern)
by <replace>.
Options:
-c - copy
Do not rename the file, but create a copy.
-f - force
Overwrite existing files.
-g - global
Replace globally (default is just the first time).
-h - help
Print help message and exit.
-n - dry run
Do not move/copy any files, only print what would happen.
-v - verbose
Print what happens.
EOF
}
warn() {
printf >&2 '%s\n' "$1"
}
missing_argument() {
warn "Missing argument for '$1'."
exit 2
}
if [ $# -eq 0 ]; then
usage >&2
exit 1
fi
args=()
copy=0
dryrun=0
force=0
help=0
replace_globally=0
verbose=0
OPTSTRING=':cfghnv'
while getopts ${OPTSTRING} opt; do
case ${opt} in
c)
copy=1
;;
f)
force=1
;;
g)
replace_globally=1
;;
h)
help=1
;;
n)
dryrun=1
;;
v)
verbose=1
;;
?)
echo >&2 "Invalid option: -${OPTARG}."
exit 1
;;
esac
done
shift $((OPTIND-1))
if (( help )); then
usage
exit 0
fi
if (( "$#" < 3 )); then
usage >&2
exit 1
fi
search_string=$1
replace_string=$2
shift 2
cmd='mv'
arrow='->'
if (( copy )); then
cmd='cp'
arrow='+>'
fi
cmdarr=( "$cmd")
if (( force )); then
cmdarr+=('-f')
fi
cmdarr+=('--')
for file; do
if [[ $search_string == '^' ]]; then
new="${replace_string}${file}"
elif [[ $search_string == '$' ]]; then
new="${file}${replace_string}"
elif (( replace_globally )); then
new=${file//$search_string/$replace_string}
else
new=${file/$search_string/$replace_string}
fi
if [[ $new == $file ]]; then
warn "Name unchanged for '$file', skipping."
continue
fi
if (( dryrun )); then
printf '%s %s %s' "$file" "$arrow" "$new"
if [[ -f "$new" ]]; then
printf '%s' ' (file exists!)'
fi
printf '\n'
continue
fi
if [[ -f "$new" ]] && ! (( force )); then
warn "Not ${cmd}-ing '${file}' to '${new}', target file exists already (use -f to overwrite)."
continue
fi
"${cmdarr[@]}" "$file" "$new"
if [[ $? -eq 0 ]]; then
(( verbose )) && printf '%s %s %s\n' "$file" "$arrow" "$new"
else
warn "Cannot ${cmd} '${file}' to '${new}'."
fi
done